Color Puzzle Game

Color Puzzle Game

Topic : Character Code Logic Game

Background:
Colour code logic game uses coloured shapes see-through tiles to form a pre-defined
formation. The player is required to stack a rack of tiles to recreate the exact composition. Itrequires logical thinking to combine shape, colour, order and decide which way round eachtile needs to be placed to solve a given puzzle. The game aims to develop logical thinkingskills, strategic planning and visual / spatial perception. 

Task:
To develop a stand-alone program with C that simulates Colour Code Logic game. The goal of thisprogram is to rearrange character tiles to solve a puzzle.

Specification & Requirement:
· Develop a 2-dimensional array in C to form a tile formation by using a selected character. You
are free to select the character and size of the tile for your game. The minimum requirement for
the tile size is 11 rows by 11 columns. The game should consist of minimum of four tiles.

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
  • You are allowed to design your own tile with characters, alphabets, numbers, colours (optional)or a combination of them. The pattern of your tile should NOT be formed randomly, you mustdesign the pattern of your tile first, then code it by using loop structure as much as you can.
    · You are expected to design your own mechanism of generating the formation of your tiles. It
    should be documented in your design report to elaborate the details of your mechanism.
    · The game will then generate a puzzle for player to solve by placing the available tiles randomlyon top of each other. The goal of the game is for the player to place the tiles in the order thatwill to recreate the exact composition of the given puzzle.
    · You may also set your own rules for your game, such as the way a block is chosen and placed.
    · The performance of the player may be measured by counting the number of attempts taken to
    solve the puzzle.
    · Each element of array contains the following suggested attributes :
    o a variable of character to form the tile
    o and any other attributes you think appropriate to your program
    Thus, the element can be described as “struct” of array in the C programming language.
    · You are expected to design your own structure for your program, and it should be documented
    in your design report to elaborate the details by listing all the functions and a brief description
    of them, which may include explanation / justification on the way you decompose your program
    into those functions / modules.
    · The record of previous players is stored in a plain text data file. The information of all the
    previous players should be displayed at the beginning of the game. User / player will also be
    prompted to key in their information, and that information will then be stored in the same text
    file.
    · Input to the system is through a DOS command window.
    · This program must first prompt the user (in the command window) for the next action to be
    carried out. The user should be able to terminatethe program at any time.
    · Output should be printed on DOS window to show the latest layout of the board.
    · The program codes MUST be in functions. DO NOT write all the code in the “main()” function.
    · Must NOT use global variable and goto in the program.
    · Graphical window interface can be used as an alternative (optional)
    · This C program is recommended to be written in Code∷Blocks or Quincy 2005.

Procedure of Program Development

Use an iterative process (spiral or prototype SDLC) to design and implement a solution to your
program. That means
1. write a small program to do a little bit of the problem
a. compile and run the small program
2. next, add a function or two
a. compile and run the improved program
3. repeat step 2 until you are done 

Snapshots:

Solution 

#include<stdio.h>

#include<conio.h>  // included for using getch() function

#include<time.h>   // included for measuring time used up by the player

#include<string.h>  // included for dealing with strings

void introduction(); //function to introduce to the Game

voidprevious_record();  // Prints the previous attempts of the Game

voidprint_tile(int tile[][11]);  // Function to print a tile

voidupdate_record(char player_name[]); // Updates the record of attempts

void create_tile1(int tile[][11]);  // Creates tile 1

void create_tile2(int tile[][11]);  // Creates tile 2

void create_tile3(int tile[][11]);  // Creates tile 3

void create_tile4(int tile[][11]);  // Creates tile 4

voidcreate_puzzle_order(intpuzzle_order[]);  // Creates a random order of tiles as a Question

void create_puzzle(int puzzle[][11],intpuzzle_order[],int tile1[][11],int tile2[][11],int tile3[][11],int tile4[][11]); // Function to create the question puzzle

voidcreate_answer_puzzle(intcurrent_puzzle[][11]); // Creates empty answer puzzle

void print_tiles(int puzzle[][11],int tile1[][11],int tile2[][11],int tile3[][11],int tile4[][11],intcurrent_puzzle[][11]); // Prints all tiles

voidupdate_current_puzzle(intcurrent_puzzle[][11],int tile[][11]);  // Updates the current answer puzzle

int main(void)

{

// Introduction

introduction();

printf(“Name\t\tNo. of Attempts\n\n”); // Printing the previous record of attempts

previous_record();

charplayer_name[20];

int score = 0;

printf(“\nPlease Enter your name:”);

scanf(“%s”,player_name);

printf(“\nGood day %s, Let’s start the game…all the best!!!\n\nPress any key to begin…\n”);

getch();

int tile1[11][11];

int tile2[11][11];

int tile3[11][11];

int tile4[11][11];

inti,j;

create_tile1(tile1); // Create tile 1

printf(“\nTile 1:\n”);

print_tile(tile1);

create_tile2(tile2);  // Create tile 2

printf(“\nTile 2:\n”);

print_tile(tile2);

create_tile3(tile3);  // Create tile 3

printf(“\nTile 3:\n”);

print_tile(tile3);

create_tile4(tile4);  // Create tile 4

printf(“\nTile 4:\n”);

print_tile(tile4);

intpuzzle_order[4];

create_puzzle_order(puzzle_order);

int puzzle[11][11];

create_puzzle(puzzle,puzzle_order,tile1,tile2,tile3,tile4);

printf(“\nPuzzle to match:\n”);

print_tile(puzzle);

int answer[4];

intcurrent_puzzle[11][11];

create_answer_puzzle(current_puzzle);

printf(“\nYour Current Puzzle:\n”);

print_tile(current_puzzle);

printf(“\n**Please Select the sequence of tiles to be placed into your puzzle**\n”);

int choice;

for(i=0;i<4;i++) // Loop for recording the answer

{

printf(“*Enter the number of your tile %d <1,2,3,4> or press 0 to quit*\n”,i+1);

scanf(“%d”,&choice);

if(choice == 0)

{

printf(“\n***********GOOD BYE %s !!!***********\n”,player_name);

return;

}

else

{

answer[i] = choice-1;

switch(choice)

{

case 1: update_current_puzzle(current_puzzle,tile1);break;

case 2: update_current_puzzle(current_puzzle,tile2);break;

case 3: update_current_puzzle(current_puzzle,tile3);break;

case 4: update_current_puzzle(current_puzzle,tile4);break;

default: printf(“\nINVALID CHOICE\n”);break;

}

print_tiles(puzzle,tile1,tile2,tile3,tile4,current_puzzle);

}

}

int flag = 0;

for(i=0;i<4;i++)  if(answer[i] != puzzle_order[i]) flag = 1; // Checking whether answer is correct or not

if(flag == 1) printf(“\n**********WRONG !!!************\n”);

elseprintf(“\n************CORRECT !!!************\n”);

update_record(player_name); // Updating the record

}

// Updates the current answer puzzle

voidupdate_current_puzzle(intcurrent_puzzle[][11],int tile[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

if(tile[i][j]!=’ ‘) current_puzzle[i][j] = tile[i][j];

}

}

}

// function which introduces the game

void introduction()

{

printf(“**************WELCOME TO LOGIC WITH CHARACTER CODE GAME**************\n\n”);

}

// function which prints the previous record

voidprevious_record()

{

FILE *fp;

charplayer_name[20];

int score;

fp = fopen(“record.txt”,”r”); // opening the file

if(fp == NULL)

{

printf(” Previous Records doesn’t exist\n”);

return ;

}

while(!feof(fp))

{

if (fscanf(fp, “%s\t\t%d”, player_name, &score) != 2) break; // fscanf returns 2 if it scans 2 variables in the file a char array and an int

printf(“%s\t\t%d\n”,player_name,score);

}

fclose(fp);

}

// Function to print a tile

voidprint_tile(int tile[][11])

{

inti,j;

printf(“\n| “);

for(i=0;i<11;i++) printf(“- “);

printf(“| \n”);

for(i=0;i<11;i++)

{

printf(“| “);

for(j=0;j<11;j++) printf(“%c “,tile[i][j]);

printf(“|\n”);

}

printf(“| “);

for(i=0;i<11;i++) printf(“- “);

printf(“| \n”);

}

// Prints all tiles

void print_tiles(int puzzle[][11],int tile1[][11],int tile2[][11],int tile3[][11],int tile4[][11],intcurrent_puzzle[][11])

{

printf(“\nTile 1:\n”);

print_tile(tile1);

printf(“\nTile 2:\n”);

print_tile(tile2);

printf(“\nTile 3:\n”);

print_tile(tile3);

printf(“\nTile 4:\n”);

print_tile(tile4);

printf(“\nPuzzle to match:\n”);

print_tile(puzzle);

printf(“\nYour Current Puzzle:\n”);

print_tile(current_puzzle);

}

voidupdate_record(char player_name[])

{

FILE *ifp; // ifp for input file and ofp is for an output file

FILE *ofp;

char name[20];

int score = 1,flag = 0;

ifp = fopen(“record.txt”,”r”);

ofp = fopen(“temp.txt”,”w”);

if(ifp == NULL)

{

fprintf(ofp,”%s\t\t%d”,player_name,score);

}

else

{

while(!feof(ifp))

{

if (fscanf(ifp, “%s\t\t%d”, name, &score) != 2) break; // fscanf returns 2 if it scans 2 variables in the file a char array and an int

if(strcmp(player_name,name) == 0)

{

fprintf(ofp,”%s\t\t%d\n”,player_name,score+1);

flag = 1;

}

else

{

fprintf(ofp,”%s\t\t%d\n”,name,score);

}

}

if(flag == 0) fprintf(ofp,”%s\t\t%d”,player_name,1);

fclose(ifp);

fclose(ofp); // by now temp.txt is filled with new record and record.txt is filled with old record

}

ifp = fopen(“temp.txt”,”r”); // to update the record.txt with temp.txt

ofp = fopen(“record.txt”,”w”);

while(!feof(ifp))

{

if (fscanf(ifp, “%s\t\t%d”, name, &score) != 2) break; // fscanf returns 2 if it scans 2 variables in the file a char array and an int

fprintf(ofp,”%s\t\t%d\n”,name,score);

}

fclose(ifp);

fclose(ofp);

}

// Creates tile 1

void create_tile1(int tile[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

tile[i][j] = ‘ ‘;

}

}

for(i=6;i<11;i++)

{

for(j=0;j<11;j++)

{

tile[i][j] = ‘*’;

}

}

}

// Creates tile 2

void create_tile2(int tile[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

tile[i][j] = ‘ ‘;

}

}

for(i=0;i<11;i++) tile[i][3] = ‘+’;

for(i=0;i<11;i++) tile[i][7] = ‘+’;

}

// Creates tile 3

void create_tile3(int tile[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

tile[i][j] = ‘ ‘;

}

}

for(i=0;i<11;i++) tile[3][i] = ‘$’;

for(i=0;i<11;i++) tile[7][i] = ‘$’;

for(i=0;i<11;i++) tile[i][5] = ‘$’;

}

// Creates tile 4

void create_tile4(int tile[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

tile[i][j] = ‘ ‘;

}

}

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

if(i+j<=5||i+j>=15||i-j<=-5||i-j>=5) tile[i][j] = ‘0’;

}

}

}

voidcreate_answer_puzzle(intcurrent_puzzle[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

current_puzzle[i][j] = ‘ ‘;

}

}

}

voidcreate_puzzle_order(intpuzzle_order[])

{

srand(time(NULL));

inti,j,random,temp;

for(i=0;i<4;i++) puzzle_order[i] = i;

for(i=3;i>=1;i–)   // randomly generating the puzzle order

{

random = rand()%(i+1);

temp = puzzle_order[random];

puzzle_order[random] = puzzle_order[i];

puzzle_order[i] = temp;

}

}

void create_puzzle(int puzzle[][11],intpuzzle_order[],int tile1[][11],int tile2[][11],int tile3[][11],int tile4[][11])

{

inti,j;

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

puzzle[i][j] = ‘ ‘;

}

}

int p;

for(p=0;p<4;p++)

{

if(puzzle_order[p] == 0)

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

if(tile1[i][j]!=’ ‘) puzzle[i][j] = tile1[i][j];

}

}

if(puzzle_order[p] == 1)

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

if(tile2[i][j]!=’ ‘) puzzle[i][j] = tile2[i][j];

}

}

if(puzzle_order[p] == 2)

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

if(tile3[i][j]!=’ ‘) puzzle[i][j] = tile3[i][j];

}

}

if(puzzle_order[p] == 3)

for(i=0;i<11;i++)

{

for(j=0;j<11;j++)

{

if(tile4[i][j]!=’ ‘) puzzle[i][j] = tile4[i][j];

}

}

}

} 

DESIGN REPORT:

1).Design of the Tiles

Tile 1: Lower Half of the Tile is filled with *  that is,

Tile[i][j] = ‘*’ if j > 5.

This can be done by running a loop in i from 6 to 10 which contains a sub-loop in j which runs from  0 to 10, in each iteration set Tile[i][j] = ‘*’.

Tile 2: Contains 2 vertical equi-spaced lines.

This can be done by running a loop in i from 0 to 10, in each  iteration set Tile[i][3] = ‘+’ and then run another loop in I from 0 to 10 , in each  iteration set Tile[i][7] = ‘+’.

Tile 3: Contains 2 Horizontal equi-spaced lines and a mid vertical line.

Developing Horizontal lines is similar to Developing Vertical lines in Tile 2. Mid Vertical line can be made by a loop in i from 0 to 10, in each iteration set Tile[5][i] = ‘$’.

Tile 4: Contains a filled Rhombus, whose sides are the lines joining the mid-points of the sides of the Tile.

2). User Manual

The Program first prints the previous attempts made by all the Players. Then it asks for the name of the Player. It then shows are the 4 tiles and creates Puzzle.

The Player has to Stack the tiles one over the other so that the top view of the Stack resembles the Puzzle.

If Player successfully stacks the tiles It prints CORRECT else it prints WRONG

3). Program Design

·         The Design of the Program is so made to have as much less code in the main function

·         Each of the tile is designed as a\described in the tile design part.

·         Random order of Tiles is needed to create the problem puzzle this is done by the following Algorithm

To shuffle an array a of n elements (indices 0..n-1):

for i from n – 1 downto 1 do

j = random integer with 0 <= j <= i

exchange a[j] and a[i]

4). Flow Chart of the Main Function