View Full Version : tic tac toe

11-29-2011, 01:01 AM
Below is my coding for Tic Tac Toe, I have hit 2 problems that I could do with a little help with.

Problem 1:

During the Game, if a player has two X or O in a diaginal and all they have to do it put 1 more to finish their 3 in a diaginal to win, but player 2 blocks it by putting his mark on the last spot in the corner, the program says that he wins! This is incorrect because he should'nt win, it should carry on until there is a clear winning. Hope this makes sence. Theres something wrong with my check function probally, but I cant seem to see it.

Problem 2:

At beginning of program, I ask for Player1 and player2 to enter their names, but when I try to get the function to output thier names, it doesnt like it. Its because its within a function, and the name input is outside the function. How Do I get the program to pass the name input into the function. So within the function I could use:

printf("&s Enter your row", player1);

Hope my problems are unstandable.

Here is my Code:

#include <stdio.h>
#include <stdlib.h>

char board[3][3]; // the tic tac toe board. Array 3 x 3

char check(void); // Function check
void init_board(void); //function inisalizing board
void get_player_move(void); //Function to get players move
void get_player2_move(void); //function to get player 2 move
void disp_board(void); //function to display board.

int main(void)
char done;

printf("Tic Tac Toe Game.\n");
printf("The World's Number 1 Online Game.\n\n");

char player1[10];
char player2[10];

printf("Player 1 Name : ", player1);
scanf("%s", &player1);
printf("Player 2 Name : ", player2);
scanf("%s", &player2);

done = ' ';

disp_board(); //Displays board after each move.
done = check(); // see if there is a winner.

if(done!= ' ') break; // winner
done = check(); // see if there is a winner after player 2 completes its move.
while(done== ' ');

if(done=='X') printf("%s has won!!!\n ", player1);
else printf("%s has won!!!!\n", player2);


// Initialize the board using an Array.
void init_board(void)
int i=0;
int j=0;

for(i=0; i<3; i++)
for(j=0; j<3; j++) board[i][j] = ' ';

// Get player 1 move

void get_player_move(void)
int x=0;
int y=0;

printf("%s, Enter Row (1-3): ", "player1");
scanf("%i", &x);
printf("%s Now Enter Column (1-3): ", "player1");
scanf("%i", &y);

x--; y--;

if(board[x][y]!= ' '){
printf("Invalid move, try again.\n");
get_player_move(); //If invalid move entered, it will go back to beginning of function(get_player_move)
else board[x][y] = 'X'; //if move is valid X will be placed onto board.

// Get player 2 move

void get_player2_move(void)
//x and y set up, x will be row, y will be column.
int x=0;
int y=0;

//Player 2 to enter thier coordinates.
printf("Player2, Enter Row (1-3): ");
scanf("%i", &x);
printf("Player2, Now Enter Column (1-3): ");
scanf("%i", &y);

x--; y--;

if(board[x][y]!= ' ')
printf("Invalid move, try again.\n");
else board[x][y] = 'O';

// Display the board on the screen

void disp_board(void)
int t=0;

for(t=0; t<3; t++)
printf(" %c | %c | %c ",board[t][0],
board[t][1], board [t][2]);
if(t!=2) printf("\n---|---|---\n");

// See if there is a winner

char check(void)
int i=0;

for(i=0; i<3; i++) // checking the rows
if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
return board[i][0];

for(i=0; i<3; i++) // checking the columns
if(board[0][i]==board[1][i] && board[0][i]==board[2][i])
return board[0][i];

// This will check diagonals
if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
return board[0][0];

if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
return board[0][2];

return '0';