Adding columns TicTacToe

How do i add more columns and rows to this tic tac toe game? E.g. making it a 4x4, 5x5 grid etc..

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <string.h>
#include <sstream>
#include <ctype.h>

using namespace std;
class game{

	private:
		int place,gameOn,turn,winner;
		char board[9];
		unsigned short int win[9];
		string posZero,posOne,posTwo,posThree, posFour,posFive, posSix,posSeven, posEight;	
	public:		
		game()
		{
			turn = 0;
			for(int i = 0; i < 9; i++)
			{
				board[i] = '\0';
				win[i] = '\0';
			}	
			cout << board[1];
		}
		int getWinner()
		{ return winner; }
		void setTurn()
		{ turn = (turn == 0) ? 1 : 0; }
		void setCords()
		{
			int x = 0;
			cout << "\n\nEnter place number: ";
			cin >> x;
			cout << "\n";
			place = x - 1;
		}
		int setGrid()
		{
			if(board[place] == 'O' || board[place] == 'X')
			{
				cout << "\nThat place is already taken!" << endl;
				draw();
				return 0;
			}	
			else if(win[place] == 1 || win[place] == 2)
			{
				cout << "\nWin already taken\n";
				draw();
				return 0;
			}
			else
			{
				if(turn == 0)
				{
					board[place] = 'X';
					win[place] = 1;
					return 1;
				}
				else
				{
					board[place] = 'O';
					win[place] = 2;
					return 1;
				} } }
		
		bool play()
		{
			for(int x = 1; x < 3; x++)
			{
				if(win[0] == x)
				{
					if(win[1] == x) { if(win[2] == x) { winner = x; return true; } }
					if(win[4] == x) { if(win[8] == x) { winner = x; return true; } }
					if(win[3] == x) { if(win[6] == x) { winner = x; return true; } }
				}
				if(win[1] == x)
				{ if(win[4] == x) { if(win[7] == x) { winner = x; return true; } } }
				if(win[2] == x)
				{
					if(win[5] == x) { if(win[8] == x) { winner = x; return true; } }
					if(win[4] == x) { if(win[6] == x) { winner = x; return true; } }
				}
				if(win[3] == x)
				{ if(win[4] == x) { if(win[5] == x) { winner = x; return true; } } }
				if(win[6] == x)
				{ if(win[7] == x) { if(win[8] == x) { winner = x; return true; } } }
			}	
			return false;	
		}
		
		void draw()
		{ cout << endl << posZero <<posOne << posTwo << posThree << posFour << posFive << posSix << posSeven << posEight; }
		void drawChange()
		{
			for(int i = 0; i < 9; i++)
			{	
				switch(i)
				{
					case 0:
						if(board[0] == 'X')
						{ posZero = "_X_|"; }
						else if(board[0] == 'O')
						{ posZero = "_O_|"; }
						else
						{ posZero = "___|"; }
						break;
					case 1:	
						if(board[1] == 'X')
						{ posOne = "_X_|"; }
						else if(board[1] == 'O')
						{ posOne = "_O_|"; }
						else
						{ posOne = "___|"; }
						break;
					case 2:
						if(board[2] == 'X')
						{ posTwo = "_X_"; }
						else if(board[2] == 'O')
						{ posTwo = "_O_"; }
						else
						{ posTwo = "___"; }
						break;
					case 3:
						if(board[3] == 'X')
						{ posThree = "\n_X_|"; }
						else if(board[3] == 'O')
						{ posThree = "\n_O_|"; }
						else
						{ posThree = "\n___|"; }
						break;
					case 4:	
						if(board[4] == 'X')
						{ posFour = "_X_|"; }
						else if(board[4] == 'O')
						{ posFour = "_O_|"; }
						else
						{ posFour = "___|"; }
						break;
					case 5:
						if(board[5] == 'X')
						{ posFive = "_X_"; }
						else if(board[5] == 'O')
						{ posFive = "_O_"; }
						else
						{ posFive = "___"; }
						break;
					case 6:
						if(board[6] == 'X')
						{ posSix = "\n X |"; }
						else if(board[6] == 'O')
						{ posSix = "\n O |"; }
						else
						{ posSix = "\n   |"; }
						break;
					case 7:	
						if(board[7] == 'X')
						{ posSeven = " X |"; }
						else if(board[7] == 'O')
						{ posSeven = " O |"; }
						else
						{ posSeven = "   |"; }
						break;
					case 8:
						if(board[8] == 'X')
						{ posEight = " X "; }
						else if(board[8] == 'O')
						{ posEight = " O "; }
						else
						{ posEight = "   "; }
						break;
					}					
				}
				cout << posZero <<posOne << posTwo << posThree << posFour << posFive << posSix << posSeven << posEight;  }	
};
int main()
{	for(;;){	
		game TTT;
		while(!TTT.play())
		{
			TTT.drawChange();
			int r = 0;
			do{ 
			TTT.setCords();
			r = TTT.setGrid();
			}while(r < 1);
			TTT.setTurn();	
		}
		TTT.drawChange();
		
		if(TTT.getWinner() == 1)
		{ cout << "\nThe Winner is X.  Congrats" << endl; }
		if(TTT.getWinner() == 2)
		{ cout << "\nThe winner is O. Congrats" << endl; }
	}
	return 0;
}
The first thing you're going to want to do is change your board from a single dimensional array (char board[9]) to a two dimension array.
1
2
static const int N = 5;
char board[N][N];


After that, you're going to want to change your functions so that they can deal with a board of arbitrary size (NxN).

Hello sorry i have posted a completely different code to the one i have open in visual studio. This is the code i have decided to work off, thanks

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
#include <stdio.h>

int main (void)
{
	int player = 0;
	int winner = 0;
	int choice = 0;
	int row = 0;
	int column = 0;
	int line = 0;

	char board [3][3] = {
				 {'1','2','3'},
				 {'4','5','6'},
				 {'7','8','9'}
						};


	for (int i = 0; i<9 && winner==0; i++)
	{
		printf("\n\n");
		printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

		player = i%2 + 1;

		do
		{
			printf("\nPlayer %d, please enter the number of the square "
				"where you want to place your %c: ",
				player,(player==1)?'X':'O');
			scanf("%d", &choice);


			row = --choice/3;
			column = choice%3;
		}while(choice<0 || choice>9 || board [row][column]>'9');

		board[row][column] = (player == 1) ? 'X' : 'O';


		if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||
		   (board[0][2]==board[1][1] && board[0][2]==board[2][0]))
		   winner = player;
		else
			for(line = 0; line <=2; line++)
				if((board[line][0]==board[line][1] && board[line][0]==board[line][2])||
					(board[0][line]==board[1][line] && board[0][line]==board[2][line]))
					winner = player;
	

	}

		printf("\n\n");
		printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);


		if(winner==0)
			printf("The game is a draw\n");
		else
			printf("Player %d has won\n", winner);

	return 0;
}
That's both a start and a regression. You've moved from C++ to C, but at least you have a two dimensional board.

You need to think about this program generically. i.e. Solve for a board of NxN where N can be any number.

This means you're going to have to change your display code to display a board of N rows and N columns. Hint: This should be a separate function.

Likewise, you may want to change your input notation from an index to row and column, but that's not critical since you can calculate row and column from index.

You will also want to change how you check for winners from hard coded positions to an algorithmic approach.

You will probably want to remove the initializer list from lines 13-15 and either display the board with row and column numbers, or initialize the board programmatically depending on the size of board.

Bottom line, any place you have hard coded array references, you're going to need to change these to do the same thing in a generic manner.

If you make the program truely generic, you could even prompt the user for what size board they want to play with.


Last edited on
Hi sorry i did not realise you replied! and im sorry for posting several times as i have just signed up to the forum and dont know how to use it.

i have this code here which i have change to create a 4x4 grid but it does not function with all the cSquare's, only 1-9 and not 1-16!

i think i may need to change some things around lines 93-122?

sorry i am only 15 but reaally want to impress my dad with the 4x4 grid!

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>

void main() {
	char cSquare1('1');
	char cSquare2('2');
	char cSquare3('3');
	char cSquare4('4');
	char cSquare5('5');
	char cSquare6('6');
	char cSquare7('7');
	char cSquare8('8');
	char cSquare9('9');
	char cSquare10('10');
	char cSquare11('11');
	char cSquare12('12');
	char cSquare13('13');
	char cSquare14('14');
	char cSquare15('15');
	char cSquare16('16');
	int iPlayerTurn(1);
	bool bGameOver(true);

	// Main game loop
	do {
		// Print board
		std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << "|" << cSquare4 << std::endl;
		std::cout << "-+-+-+-"<< std::endl;
		std::cout << cSquare5 << "|" << cSquare6 << "|" << cSquare7 << "|" << cSquare8 << std::endl;
		std::cout << "-+-+-+-"<< std::endl;
		std::cout << cSquare9 << "|" << cSquare10 << "|" << cSquare11 << "|" << cSquare12 << std::endl;
		std::cout << "-+-+-+-"<< std::endl;
		std::cout << cSquare13 << "|" << cSquare14 << "|" << cSquare15 << "|" << cSquare16 << std::endl;

		// Set player marker: Player 1 uses X and Player 2 uses O
		char cPlayerMark;
		if (iPlayerTurn == 1) {
			cPlayerMark = 'X';
		} else {
			cPlayerMark = 'O';
		}
		
		// Prompt the player for a move
		std::cout << "Player" << iPlayerTurn << "'s move:" << std::endl;
		bool bValidMove;
		// Loop until we get a valid move
		do {
			char cNextMove;
			std::cin >> cNextMove;
			bValidMove = true;

			// Check for a valid move
			if (cNextMove == '1' && cSquare1 == '1') {
				cSquare1 = cPlayerMark;
			} else if (cNextMove == '2' && cSquare2 == '2') {
				cSquare2 = cPlayerMark;
			} else if (cNextMove == '3' && cSquare3 == '3') {
				cSquare3 = cPlayerMark;
			} else if (cNextMove == '4' && cSquare4 == '4') {
				cSquare4 = cPlayerMark;
			} else if (cNextMove == '5' && cSquare5 == '5') {
				cSquare5 = cPlayerMark;
			} else if (cNextMove == '6' && cSquare6 == '6') {
				cSquare6 = cPlayerMark;
			} else if (cNextMove == '7' && cSquare7 == '7') {
				cSquare7 = cPlayerMark;
			} else if (cNextMove == '8' && cSquare8 == '8') {
				cSquare8 = cPlayerMark;
			} else if (cNextMove == '9' && cSquare9 == '9') {
				cSquare9 = cPlayerMark;
			} else if (cNextMove == '10' && cSquare10 == '10') {
				cSquare10 = cPlayerMark;
			} else if (cNextMove == '11' && cSquare9 == '11') {
				cSquare11 = cPlayerMark;
			} else if (cNextMove == '12' && cSquare12 == '12') {
				cSquare12 = cPlayerMark;
			} else if (cNextMove == '13' && cSquare13 == '13') {
				cSquare13 = cPlayerMark;
			} else if (cNextMove == '14' && cSquare14 == '14') {
				cSquare14 = cPlayerMark;
			} else if (cNextMove == '15' && cSquare15 == '15') {
				cSquare15 = cPlayerMark;
			} else if (cNextMove == '16' && cSquare16 == '16') {
				cSquare16 = cPlayerMark;
			} else {
				std::cout << "Invalid Move. Try again." << std::endl;
				bValidMove = false;
			}
		} while (!bValidMove);

		bGameOver		= false;
		bool bWinGame	= true;
		// Check for end of game conditions
		if (cSquare1 != '1') {
			if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
				bGameOver = true;
			}
			if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
				bGameOver = true;
			}
		}
		if (cSquare5 != '5') {
			if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
				bGameOver = true;
			}
			if (cSquare2 == cSquare5 && cSquare8 == cSquare5) {
				bGameOver = true;
			}
			if (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
				bGameOver = true;
			}
			if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
				bGameOver = true;
			}
		}
		if (cSquare9 != '9') {
			if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
				bGameOver = true;
			}
			if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
				bGameOver = true;
			}
		}
		// Need to check the board full (no-win condition)
		if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' && cSquare4 != '4'
			cSquare5 != '5' && cSquare6 != '6' && cSquare7 != '7' && cSquare8 != '8'
			cSquare9 != '9' && cSquare10 != '10' && cSquare11 != '11' && cSquare12 != '12'
			cSquare13 != '13' && cSquare14 != '14' && cSquare15 != '15' && cSquare16 != '16' !bGameOver)
		{
			bGameOver = true;
			bWinGame = false;
		}

		if (bGameOver) {
			if (bWinGame) {
				std::cout << "Player" << iPlayerTurn << " wins!" << std::endl;
			}
			// Print ending board
			std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << "|" << cSquare4 << std::endl;
			std::cout << "-+-+-+-"<< std::endl;
			std::cout << cSquare5 << "|" << cSquare6 << "|" << cSquare7 << "|" << cSquare8 << std::endl;
			std::cout << "-+-+-+-"<< std::endl;
			std::cout << cSquare9 << "|" << cSquare10 << "|" << cSquare11 << "|" << cSquare12 << std::endl;
			std::cout << "-+-+-+-"<< std::endl;
			std::cout << cSquare13 << "|" << cSquare14 << "|" << cSquare15 << "|" << cSquare16 << std::endl;

			std::cout << "Game Over!" << std::endl;
			std::cout << "Play again (y/n)?" << std::endl;
			char cPlayAgain;
			std::cin >> cPlayAgain;

			if (cPlayAgain == 'y') {
				bGameOver = false;
				// Clear the board
				cSquare1 = '1';
				cSquare2 = '2';
				cSquare3 = '3';
				cSquare4 = '4';
				cSquare5 = '5';
				cSquare6 = '6';
				cSquare7 = '7';
				cSquare8 = '8';
				cSquare9 = '9';
				cSquare10 = '10';
				cSquare11 = '11';
				cSquare12 = '12';
				cSquare13 = '13';
				cSquare14 = '14';
				cSquare15 = '15';
				cSquare16 = '16';
			}
			iPlayerTurn = 1;
		} else {
			// Alternate player turns
			if (iPlayerTurn == 1) {
				iPlayerTurn = 2;
			} else {
				iPlayerTurn = 1;
			}
		}
	} while (!bGameOver);
}
Lines 13-16 are not valid. You can't put two characters from a character literal into a char.

You're not thinking about this as a generic problem. You have regressed from a [3][3] array to a bunch of single character variables. That does not lend itself to creating a board of any dimension.

Programming is about algorithms. Think about how to do each step as an algorithm, rather than thinking about absolute positions with fixed dimensions.
Last edited on
your first problem:
void main()
main() should return an int, if your compiler supports it, it is EXTREMELY outdated.

sorry i am only 15 but reaally want to impress my dad with the 4x4 grid!

Age doesn't matter. I am 13 and I am making FPS games.

Now, first, you should represent your gameboard as a class. If you aren't up to the OOP lingo, you can just use a two dimensional array.

 
gameBoard[4][4]


you don't really need the lines as that just makes it harder, but you can add more elements for them if you really want.
Last edited on
You really should start writing your own code instead of copying someone elses. Those three examples that may be found with help from google very easily are bullcrap. The first one seems like C++, but isn't scaleable and uses one dimensional array which makes code more complex. The second one is written in pure C. The third one is the worst: variables are cIt and iThat and it uses variables to present the board instread of some kind of array. And as mentioned before, the code is broken and it uses void main().

As mentioned before, use algorithms and write them in functions. If this is too hard, you should try writing easier algorithms first. You should search some tutorial about them.

If you want to post already existing example, you can just give a link. It makes topic easier to read when you don't have to skip code parts to read the conversation.
Topic archived. No new replies allowed.