Tic Tac Toe

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 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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 =  ' ';
	init_board();

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

		if(done!= ' ') break; // winner
		get_player2_move();
		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);  
	system("pause");

}

// 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");
		get_player2_move();
	}
	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");
	}
	printf("\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';
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#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 = ' ';
init_board();

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

if(done!= ' ') break; // winner
get_player2_move();
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); 
system("pause");

}

// 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");
get_player2_move();
}
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");
}
printf("\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';
}



Please, put your code in the [ code ] tags

I'm not fluent with any C, so this is going to be 10x harder for me.
Last edited on
anyone got any help for me?
I don't really see you having a different function printing out the winner in your code though. It still seems to be inside the main function.

Anyway, there are 2 simple ways to achieve what you want to do though.

1. make your player names global variables
2. modify your function so it has parameters

The first one is bad practice and not recommended, so it's best if you stick to number 2.

Example:

1
2
3
4
5
6
7
8
9
10
11
void PrintWinner(char player1[], char player2[], char done)
{
    if(done=='X')
    {
        printf("%s has won!!!\n ", player1);
    }
    else
    {
        printf("%s has won!!!!\n", player2);
    }
}


then you call it inside main like so:

PrintWinner(player1, player2, done);
i'm a beginner myself but it looks like you initialize 'done' with a value of ' ' (whitespace), but your check() function changes that value to '0' even if none of the conditions are met. i posted a working 3D tic tac toe game a few days back which you're welcome to check out if you want.

http://www.cplusplus.com/forum/beginner/55985/

i approached it a little differently. while i also used a global array, i populated it with integers right off the bat. i had 0s represent an unoccupied square, 1s represent Xs, 2s represent Os, and 5s represent boundaries for check purposes. i also incremented a value for 'turn' every time a move was made. if that value was an odd number it was Xs turn. if it was an even number, it was Os turn etc.
Dacster13 I tried that but its still not working.

In main I have

char player1[10]
Printf Players name
scanf &player1

for the player move function I have

void playermove(char player1 [], char done)

then in main I have

playermove(player1, done)


kinggrunt, on your check function it ends with "return 'O';" no matter who's won.
Last edited on
Topic archived. No new replies allowed.