Reputation: 357
I wrote a tic tac toe game, it is not against the computer. In a nutshell, the user enters a location on the board (3X3) and that location is converted to x
or o
(depends on the turn of x and o). If x or o fills a column, row or a diagonal - it wins.
I'm faced with some problems in my code,
1. The matrix doesn't show up properly, it puts every chosen location in the same location.
2. The program starts with x as it should do, but skips o.
I would be happy if you'll explain where I did go wrong.
Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define COL 3
#define ROW 3
int validity(char board[][COL], int player,int row,int col);
void turns(char board[][COL],int player);
int win(char board[][COL]);
void printBoard(char board[][COL]);
/*
main will determine the turn of x and o until there is a victory
in: none
out: 0
*/
int main(void)
{
char board[ROW][COL] = { { 0 } };
int player = 'o';
do{
if (player == 'x')
{
player = 'o';
turns(board,player);
}
else if (player == 'o')
{
player = 'x';
turns(board,player);
}
} while (win(board) == 0);
getchar();
return 0;
}
/*this func will print the board
in: board
out: none
*/
void printBoard(char board[][COL])
{
int i = 0, j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
/*
this func will assign 'x' or 'o' to the entered location on the board
in: the board and x or o
out: none
*/
void turns(char board[][COL],int player)
{
int row = 0, col = 0;
printf("It is %c turn's. \n", player);
printf("Enter a location on the board: \n");
scanf("%d", &board[row][col]);
if (validity(board,player,row,col) == 1)
{
board[row][col] = player;
}
else
{
turns(board, player);
}
printBoard(board);
}
/*
this func will determine if x or o has won
in: the board
out: 0 = noone has won
1 = x or o has won
*/
int win(char board[][COL])
{
int flag = 0;
int i = 0, j = 0, k = 0;
for (i = 0; i < ROW; i++) // this loop will check if o or x won by filling a row
{
if (board[i][j] == 'x')
{
printf("X won!");
flag = 1;
}
else if (board[i][j] == 'o')
{
printf("O won!");
flag = 1;
}
else
{
flag = 0;
}
}
for (j = 0; j < COL; j++) // this loop will check if o or x won by filling a column
{
if (board[i][j] == 'x')
{
printf("X won!");
flag = 1;
}
else if (board[i][j] == 'o')
{
printf("O won!");
flag = 1;
}
else
{
flag = 0;
}
}
for (k = 0; k < COL; k++) // this loop will check if x or o won by filling a diagonal
{
if (board[k][k] == 'x')
{
printf("X won!");
flag = 1;
}
else if (board[k][k] == 'o')
{
printf("O won!");
flag = 1;
}
else
{
flag = 0;
}
}
return flag;
}
/*this func will check the validity of a choice that the player has made (if the location is taken by o or x or not).
in: board, x / o, the location
out: 0 = the location is taken
1 = the location is valid
*/
int validity(char board[][COL], int player,int row,int col)
{
int check = 0;
if (board[row][col] == 'o' && player == 'x' || board[row][col] == 'x' && player == 'o')
{
printf("Invalid choice.\n");
check = 0;
}
else
{
check = 1;
}
return check;
}
Upvotes: 1
Views: 78
Reputation: 1493
There is a misktake while taking input of locations in turns
function.
The function should be as follows
void turns(char board[][COL],int player)
{
int row = 0, col = 0;
printf("It is %c turn's. \n", player);
printf("Enter a location on the board: \n");
scanf("%d %d", &row,&col);
if (validity(board,player,row,col) == 1)
{
board[row][col] = player;
}
else
{
turns(board, player);
}
printBoard(board);
}
Your's was
int row=0,col=0;
scanf("%d",&board[row][col])
where row and column are always zero.
Upvotes: 1