Purpose: Use a 2-D array. Write functions to process a 2-D array Description: Al
ID: 3639292 • Letter: P
Question
Purpose: Use a 2-D array.Write functions to process a 2-D array
Description:
Allow a user(s) to enter row and column values to populate a
tictactoe grid.
This assignment does not require actual game play, just entry
of row,column pairs to populate the grid. (The game is the
next assignment.)
Ex:
--
Input:
0 0
Output:
| |
X | |
| |
-----------------
| |
| |
| |
-----------------
| |
| |
| |
Notes:
-----
- Read only the integer row and column values
- Read until the board is full, or a row,column
pair of -1,-1 is entered.
- The three row and column values will be 0,1,2
(C style counting.)
- Display the grid after each row,column entry is made
- Write functions to:
- read the row and column values
- populate the grid
- check for a full grid
- check if a square in the grid is open
- Attempt to keep main( ) as "clean" as possible
- Test your program with a variety of data sets that match the
problem description.
- X always goes first.
- Alternate X/O.
** NOTE THE EXAMPLE LOOKS DISTORTED IT SHOULD LOOK LIKE A TIC TAC TOE GRID
Explanation / Answer
please rate - thanks
if this isn't what you need, message me before rating
#include <stdio.h>
int check(char [][3]);
void init_matrix(char[][3] );
int get_player_move(char,char[][3]);
void disp_matrix( char[][3]);
int main( )
{char p='X';
char matrix[3][3];
int done;
init_matrix(matrix);
disp_matrix(matrix);
disp_matrix(matrix);
do {done=get_player_move(p,matrix);
if(done==0)
{printf("after player move ");
if(p=='X')
p='O';
else
p='X';
disp_matrix(matrix);
done = check(matrix);
}
} while(done==0);
printf("game over");
getch();
return 0;
}
void init_matrix(char matrix[][3] )
{ int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++)
matrix[i][j] =' ';
}
int get_player_move(char p,char matrix[][3] )
{ int x, y,m;
int i,j;
do
{
printf("enter a row 1-3 for your move: ");
scanf("%d",&i);
while((i<1||i>3)&&i!=-1)
{printf("Invalid input-try again ");
printf("enter a row 1-3 for your move: ");
scanf("%d",&i);
}
printf("enter a column 1-3 for your move: ");
scanf("%d",&j);
while((j<1||j>3)&&i!=-1)
{printf("Invalid input-try again ");
printf("enter a column 1-3 for your move: ");
scanf("%d",&j);
}
if(i==-1&&j==-1)
return 1;
i--;
j--;
if(matrix[i][j]=='X'||matrix[i][j]=='O')
printf("Invalid move, try again. ");
}while(matrix[i][j]=='X'||matrix[i][j]=='O');
matrix[i][j] = p;
return 0;
}
void disp_matrix(char matrix[][3] )
{
int t;
for(t=0; t<3; t++) {
printf(" %2c | %2c | %2c",matrix[t][0], matrix[t][1], matrix [t][2]);
if(t!=2) printf(" ----|----|---- ");
}
printf(" ");
}
int check(char matrix[][3] )
{int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(matrix[i][j]==' ')
return 0;
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.