Making a game with a 1D Array

Pages: 12
I have to make a tic-tac-toe game with a 1D array.

I was told to make the array type char.

This is my code so far

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
void get_input (char []);

void showBoard (char [], int, int);

void winning (char [], int, int);

int main ()
{	
	int playerX(0), playerO(0);
	char gameboard[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

	cout << "This is a game of tic-tac-toe.  You will choose your selection by"
		 << " entering the corresponding number (1-9) on the gameboard.\n\n"; 

	showBoard (gameboard, playerX, playerO);

	cout << "\nPlayer X will select first.\n";

	get_input (gameboard);

	winning (gameboard, playerX, playerO);



	return 0;
}


void get_input (char gameboard[])
{
	for (int i = 1; i <= 9; i++)
	{
		int playerX(0), playerO(0);

		cout << "What is your choice player X? ";
		cin >> playerX;
		showBoard (gameboard, playerX, playerO);
		cout << "What is your choice player O? ";
		cin >> playerO;
		showBoard (gameboard, playerX, playerO);
	}
}

void showBoard (char a[], int playerX, int playerO)
{
	cout << '1' << "  " << '2' << "  " << '3' << endl;
	cout << '4' << "  " << '5' << "  " << '6' << endl;
	cout << '7' << "  " << '8' << "  " << '9' << endl;
}
	
void winning (char gameboard[], int playerX, int playerO)
{
	cout << "You won player X!";
}


I'm not sure how to manipulate the gameboard so that I can display the gameboard after each time a player makes a choice. I wanted to use the array elements to print out the gameboard, but I'm not sure how to do that or if that is even the best way to do it.

Also I'm not sure how to check to see if a player won or if it was a draw. Would I use a switch with a condition such as (1==2==3==true)? So would I make a condition for all 8 possible winning combinations? Would I have to use 16 conditions to check for both player X and player O?

Show board is generally:
cout << a[0] << "|" << a[1] << "|" << a[2] << endl; //...

You don't need to pass anything else to that function, the contents in the array are blank (or a number to show what input means what), and the 'x' or 'o' that might exist there.

Would I use a switch with a condition such as (1==2==3==true)


Heavens no. I used a switch for the "game state" (continue, draw, winner), but that was about it as far as switches went.

Would I have to use 16 conditions to check for both player X and player O?

Yes.
Last edited on
when I used cout to display the specific array elements I got weird characters when in the output. Do anybody know that is occurring?

How do I make it so the showboard function displays an updated version of the gameboard after each selection by a player?
Last edited on

That's the link to another person having a problem with their code. They are making an array that has tictactoe as well. His problem however is that he can't display "winner"
You guys each have a problem with a similar project, maybe you can help each other.
Lol, maybe you guys are even working on the same project for the same class or something of that nature
Thanks. That person is using a bool function for some of his or her functions. I'm not sure f that is the best option.

I think my only problem right now is that I don't know how to manipulate and show the gameboard after each selection. I would really appreciate if someone could help so I don't end up wasting a ton of time I don't have.
I edited my code. My output is beneath the code. The problem seems to be that if I enter one number that array element is not changed until the next player (playerO) types a number. No matter what number playerO types the array element of the first number entered is changed to X. Does anybody know why this is occuring and how it can be fixed?

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
#include <iostream>
using namespace std;

void get_input (char []);

void showBoard (char [], int, int);

void winning (char [], int, int);

int main ()
{	
	int playerX(0), playerO(0);
	char gameboard[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

	cout << "This is a game of tic-tac-toe.  You will choose your selection by"
		 << " entering the corresponding number (1-9) on the gameboard.\n\n"; 

	showBoard (gameboard, playerX, playerO);

	cout << "\nPlayer X will select first.\n";

	get_input (gameboard);

	winning (gameboard, playerX, playerO);



	return 0;
}


void get_input (char a[])
{
	for (int i = 1; i <= 9; i++)
	{
		int playerX(0), playerO(0);

		cout << "What is your choice player X? ";
		cin >> playerX;
		showBoard (a, playerX, playerO);
		cout << "What is your choice player O? ";
		cin >> playerO;
		showBoard (a, playerX, playerO);
	}
}

void showBoard (char a[], int playerX, int playerO)
{
	cout << a[0] << "  " << a[1] << "  " << a[2] << endl;
	cout << a[3] << "  " << a[4] << "  " << a[5] << endl;
	cout << a[6] << "  " << a[7] << "  " << a[8] << endl;
	if (playerX == 1)
		a[0] = 'X';
	else if (playerX == 2)
		a[1] = 'X';
	else if (playerX == 3)
		a[2] = 'X';
	else if (playerX == 4)
		a[3] = 'X';
	else if (playerX == 5)
		a[4] = 'X';
	else if (playerX == 6)
		a[5] = 'X';
	else if (playerX == 7)
		a[6] = 'X';
	else if (playerX == 8)
		a[7] = 'X';
	else if (playerX == 9)
		a[8] = 'X';
	else if (playerO == 1)
		a[0] = 'O';
	else if (playerO == 2)
		a[1] = 'O';
	else if (playerO == 3)
		a[2] = 'O';
	else if (playerO == 4)
		a[3] = 'O';
	else if (playerO == 5)
		a[4] = 'O';
	else if (playerO == 6)
		a[5] = 'O';
	else if (playerO == 7)
		a[6] = 'O';
	else if (playerO == 8)
		a[7] = 'O';
	else if (playerO == 9)
		a[8] = 'O';
}
	
void winning (char a[], int playerX, int playerO)
{
	// 16 winning conditions between two players
	if (a[0]==a[1]==a[2]=='X')
		cout << "You won player X!";
	else if (a[3]==a[4]==a[5]=='X')
		cout << "You won player X!";
	else if (a[6]==a[7]==a[8]=='X')
		cout << "You won player X!";
	else if (a[0]==a[3]==a[6]=='X')
		cout << "You won player X!";
	else if (a[1]==a[4]==a[7]=='X')
		cout << "You won player X!";
	else if (a[2]==a[5]==a[8]=='X')
		cout << "You won player X!";
	else if (a[0]==a[4]==a[8]=='X')
		cout << "You won player X!";
	else if (a[2]==a[4]==a[6]=='X')
		cout << "You won player X!";
	else if (a[0]==a[1]==a[2]=='O')
		cout << "You won player O!";
	else if (a[3]==a[4]==a[5]=='O')
		cout << "You won player O!";
	else if (a[6]==a[7]==a[8]=='O')
		cout << "You won player O!";
	else if (a[0]==a[3]==a[6]=='O')
		cout << "You won player O!";
	else if (a[1]==a[4]==a[7]=='O')
		cout << "You won player O!";
	else if (a[2]==a[5]==a[8]=='O')
		cout << "You won player O!";
	else if (a[0]==a[4]==a[8]=='O')
		cout << "You won player O!";
	else if (a[2]==a[4]==a[6]=='O')
		cout << "You won player O!";
	else
		cout << "It’s a draw.  No one won.";  
}



This is a game of tic-tac-toe.  You will choose your selection by entering the c
orresponding number (1-9) on the gameboard.

1  2  3
4  5  6
7  8  9

Player X will select first.
What is your choice player X? 1
1  2  3
4  5  6
7  8  9
What is your choice player O? 3
X  2  3
4  5  6
7  8  9
What is your choice player X? 5
X  2  3
4  5  6
7  8  9
What is your choice player O? 9
X  2  3
4  X  6
7  8  9
What is your choice player X?
Last edited on
Try using a globally declared gameboard, that way you can assign values to the individual element you want, I don't think you can assign values like "a={1,2,3...etc};" after declaration. Try
 
if(PlayerX==3) { gameboard[PlayerX-1] = 'X';}


Also, you have to declare variables before you use them (in 'get_input' 'i' isn't declared)
Last edited on
For my class I don't think I'm allowed to globally declare variables. I tried that anyway though, I still have the same issue. I'm thinking it might be because I'm calling the function showboard twice inside one for loop.

I declared "i" within the for loop. Is that a bad technique?
Last edited on
I changed the get_input function and I'm still getting the same issue. I changed the for loop to a while loop because I didn't want to the game to keep going if someone won. I don't think what i did eliminates that issue though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void get_input (char a[])
{
	int choice(0);
	while (choice < 6)
	{
		int playerX(0), playerO(0);

		cout << "What is your choice player X? ";
		cin >> playerX;
		showBoard (a, playerX, playerO);
		cout << "What is your choice player O? ";
		cin >> playerO;
		showBoard (a, playerX, playerO);
		choice++;
	}
}
Last edited on
Can anybody help. I'm losing so much time to this. I think it's something simple I can't see since I don't have a lot of experience.
Last edited on
They are making an array that has tictactoe as well. His problem however is that he can't display "winner"


Tic-Tac-Toe seems to be the "beginner" game of choice (I know it was for me).

I think you have all of the code that you need, the problem is that it is in the wrong places.

The simplest way seems to be to declare a bool to determine whos turn it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool playerXTurn = true;
int choice;
while (playing)
{
  showboard(board);
  
  bool invalidChoice = true;
  do
  {
    if (playerXTurn) cout << "It's your turn X: ";
    else cout << "What's it going to be O? ";
    cin >> choice;
    if (choice > 0 && choice < 9) // && board position isn't used
      invalidChoice = false;
  }
  while (invalidChoice);

  if (playerXTurn) board[choice] = 'X';
  else board[choice] = 'O';

  // Figure out if game is over
}


showboard() does nothing to the array, it only shows it.
Last edited on
What would I use for the while loop to execute? Edit: nevermind this. I thought the first while loop was necessary and the code you was just a function that would be part of a program.

For what I have to do the showboard function has to show the gameboard at the beginning of the game, after each player choice, and at the end of the game. I need something to manipulate the array. The code is due at 11 pm.
Last edited on
Yeah, that makes sense, you just need to separate your if else chain, so if it's playerX's turn it only checks playerX's winning conditions. The way you have it it's checking both at the same time. Since PlayerX is checked first, PlayerO is ignored. Just pass an extra parameter to winning to ensure your function knows which player is being checked. Also, put the code for displaying the board after the array is set.

EDIT: Seperate the if else statements in showboard. :)
Last edited on
I'm a little lost between what you two guys are saying.

I need showboard to be able to show the board at the beginning, and end, and after each choice. So showboard has to meet conditions/qualifications.

I don't know how to separate the winning conditions. I need to get input from player X, then manipulate the array, then show the board, then get input from player O, then manipulate the array, then show the board. I need to continue that until there is a winner than I show the board one last time (or I guess just leave the board showing) and then tell who won or if it was a draw.

Are you guys saying to create another function that manipulates the gameboard and just use showboard to display the changed board?
Last edited on
Basically, instead of passing both PlayerX and PlayerO to showboard, just pass a parameter that says which player is setting a mark (bool?). You are displaying the board before setting what changes the player made.
I tried so many combos of the if else statements in showboard. I can't think of that extra parameter, either because the deadline is 11 pm and I'm rushed or I just can't code it. I feel like I'm close though and what to get this.

I told see how in LowestOne's code player O will ever be prompted for input.
Last edited on
You don't have to change winning... that was a typo.

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
void showBoard (char a[], bool whichplayer, int choice)
{
	if(whichplayer == true) { //set to true if it's playerx
        if (choice == 1)
		a[0] = 'X';
	else if (choice == 2)
		a[1] = 'X';
	else if (choice == 3)
		a[2] = 'X';
	else if (choice == 4)
		a[3] = 'X';
	else if (choice == 5)
		a[4] = 'X';
	else if (choice == 6)
		a[5] = 'X';
	else if (choice == 7)
		a[6] = 'X';
	else if (choice == 8)
		a[7] = 'X';
	else if (choice == 9)
		a[8] = 'X';
        else cout << "Invalid choice!" << endl;
        }else { // if it's not playerx it's playero
	else if (choice == 1)
		a[0] = 'O';
	else if (choice == 2)
		a[1] = 'O';
	else if (choice == 3)
		a[2] = 'O';
	else if (choice == 4)
		a[3] = 'O';
	else if (choice == 5)
		a[4] = 'O';
	else if (choice == 6)
		a[5] = 'O';
	else if (choice == 7)
		a[6] = 'O';
	else if (choice == 8)
		a[7] = 'O';
	else if (choice == 9)
		a[8] = 'O';
        else cout << "Invalid choice!" << endl;
        }
	cout << a[0] << "  " << a[1] << "  " << a[2] << endl;
	cout << a[3] << "  " << a[4] << "  " << a[5] << endl;
	cout << a[6] << "  " << a[7] << "  " << a[8] << endl;
}


Or something like that
Last edited on
Alright, I have this now. The compiler is telling me choice is being used without being initialized. What would I initialize it too?


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
void get_input (char a[], int choice)
{
	for (int i = 1; i <= 9; i++)
	{
		int playerX(0), playerO(0);

		cout << "What is your choice player X? ";
		cin >> choice;
		showBoard (a, playerX, playerO);
		cout << "What is your choice player O? ";
		cin >> choice;
		showBoard (a, playerX, playerO);
	}
}

void showBoard (char a[], bool whichplayer, int choice)
{
	if(whichplayer == true) 
	{
        if (choice == 1)
			a[0] = 'X';
		else if (choice == 2)
			a[1] = 'X';
		else if (choice == 3)
			a[2] = 'X';
		else if (choice == 4)
			a[3] = 'X';
		else if (choice == 5)
			a[4] = 'X';
		else if (choice == 6)
			a[5] = 'X';
		else if (choice == 7)
			a[6] = 'X';
		else if (choice == 8)
			a[7] = 'X';
		else if (choice == 9)
			a[8] = 'X';
        else cout << "Invalid choice!" << endl;
	}
	else
	{
		if (choice == 1)
			a[0] = 'O';
		else if (choice == 2)
			a[1] = 'O';
		else if (choice == 3)
			a[2] = 'O';
		else if (choice == 4)
			a[3] = 'O';
		else if (choice == 5)
			a[4] = 'O';
		else if (choice == 6)
			a[5] = 'O';
		else if (choice == 7)
			a[6] = 'O';
		else if (choice == 8)
			a[7] = 'O';
		else if (choice == 9)
			a[8] = 'O';
        else 
			cout << "Invalid choice!" << endl;
	}
	cout << a[0] << "  " << a[1] << "  " << a[2] << endl;
	cout << a[3] << "  " << a[4] << "  " << a[5] << endl;
	cout << a[6] << "  " << a[7] << "  " << a[8] << endl;
	

}
	
void winning (char a[])
{
	// 16 winning conditions between two players
	if (a[0]==a[1]==a[2]=='X')
		cout << "You won player X!";
	else if (a[3]==a[4]==a[5]=='X')
		cout << "You won player X!";
	else if (a[6]==a[7]==a[8]=='X')
		cout << "You won player X!";
	else if (a[0]==a[3]==a[6]=='X')
		cout << "You won player X!";
	else if (a[1]==a[4]==a[7]=='X')
		cout << "You won player X!";
	else if (a[2]==a[5]==a[8]=='X')
		cout << "You won player X!";
	else if (a[0]==a[4]==a[8]=='X')
		cout << "You won player X!";
	else if (a[2]==a[4]==a[6]=='X')
		cout << "You won player X!";
	else if (a[0]==a[1]==a[2]=='O'
drognisep, can you help me finish this up. I have a little over 10 minutes left. I can't even compile now. I don't think there is anyway I can get this done in 10 minutes. Amazing how the hours just pass by and there is so much work still to do.

I tried just initializing choice to 1 just to get something going and now after hitting any key I get an error that says whichplayer is being used without being initialized.
Last edited on
.
Last edited on
Pages: 12