Program skips out code....

Hello,

I'm trying to make it so the user has a choice between a 1 and 2 player game in a tic-tac-toe game i made....

It worked fine when it was just 1-player against the computer... to make it 2-player I made a function that lets the user select the number of players which returns a char value '1' or '2' ( askPlayer() ).

If the char == '1' enter one loop, if char =='2' enter the other loop.... However it just seems to skip out the loops altogether....

The main code is below... Any help would be great.... 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
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
197
198
199
200
201
202
203
#include <iostream>
#include <string>
#include <cctype>
#include <istream>

#include "Player.h"
#include "Computer.h"
#include "Gameboard.h"

using namespace std;

const char x = 'X';
const char o = 'O';
const char noOne = 'N';
const char tie = 'T';
void const instructions();
void pieceAsk();
void resultPrint();
char askPlayer();
char onePiece;
char compPiece;
char playerNum;
char turn;
char winner;
char thisMove;
char test;


int main() 
{
	system("clear");
	playerNum = 1;
	Gameboard boardObj;
	Player playerOne(onePiece);
	Player playerTwo(compPiece);
	Computer comp(compPiece);
    
	
	instructions();
	boardObj.printBoard();
	playerNum = askPlayer();
	pieceAsk();

	turn = x;
	
	if(playerNum == '1')
	{

		while(winner == noOne)
		{

			if(turn == onePiece)
			{

				playerOne.print();
				boardObj.makeMove(playerOne.playerMove(boardObj), onePiece);
				boardObj.printBoard();
				turn = compPiece;
				winner = boardObj.checkWinner(onePiece);
				cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO\n"<< endl;

				
			}
			else if(turn == compPiece)
			{
			
				boardObj.makeMove(comp.compMove(boardObj), compPiece);
				system("clear");
				cout << "\nThe computer has moved...\n" << endl;
				boardObj.printBoard();
				turn = onePiece;
				winner = boardObj.checkWinner(compPiece);
				cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO\n"<< endl;
			}
			else
			{
				printf("Something's gone wrong");
			}
		}
	}
	
	else if(playerNum == '2')
	{
		while(winner == noOne)
		{
			cout << "while(winner == noOne)" << endl;
			if(turn == onePiece)
			{
				cout << "if(turn == onePiece)" << endl;
				playerOne.print();
				boardObj.makeMove(playerOne.playerMove(boardObj), onePiece);
				boardObj.printBoard();
				turn = compPiece;
				winner = boardObj.checkWinner(onePiece);
				cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO\n"<< endl;
			}
			if(turn == compPiece)
			{
				cout << "if(turn == compPiece)" << endl;
				playerTwo.print();
				boardObj.makeMove(playerTwo.playerMove(boardObj), compPiece);
				boardObj.printBoard();
				turn = onePiece;
				winner = boardObj.checkWinner(compPiece);
				cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO\n"<< endl;			}
			else 
			{
				printf("Something's gone wrong");
			}
		}
	}
		
	system("clear");
	cout << "\nXOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO\n"<< endl;
	boardObj.printBoard();
	resultPrint();

    return 0;
}


void const instructions()
{
	cout << endl;
	cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO"<< endl;
	cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO"<< endl;
	cout << "XOXOXOXOXOXO| Noughts and Crosses Extreme |OXOXOXXOXOXO" << endl;
	cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO"<< endl;
	cout << "XOXOXOXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXOXOXOXXXOXOXO"<< endl;


	cout << "\nRules: Match three in a row vertically, horizontally or \n\tdiagonally. X's go first.\n"<<endl;
}


void pieceAsk()
{
	char choice;
	cout << "Would you like to be Os or Xs (Enter O or X): ";
	cin >> choice;
	choice = toupper(choice);
	
	while((choice != o) && (choice != x))
	{
		cout << "Please enter a valid answer (X or O): ";
		cin >> choice;
		choice = toupper(choice);
	}
		
	if(choice == x)
	{
	
		onePiece = x;
		compPiece = o;
	}
	else if(choice == o)
	{

		onePiece = o;
		compPiece = x;
	}
	else
	{
		cout << "\nERROR. DEFAULT SETTING: You will play as X's\n";
		onePiece = x;
		compPiece = o;
	}
}

void resultPrint()
{

	if(winner == tie)
	{
		cout << "\nOh dear... it was a draw..." << endl;
	}
	else if(winner == compPiece)
	{
		cout << "\nComputer wins... feeble humanoid" << endl;
	}
	else 
	{
		cout << "\nWell done " << winner << "'s win!!!... Clever..." << endl;
	}
}

char askPlayer()
{
	string playerChar;
	cout << "\nHow many players? (1 or 2): ";
	cin >> playerChar;
	while((!isdigit(playerChar[0])) || (playerChar.length() > 1)
		  || ((playerChar[0] != '1') && (playerChar[0] != '2')))
	{
		cout << "Please enter a valid number (1 or 2): ";
		cin >> playerChar;
	}
	cout << "Number of players: "<< playerChar;
	cout << endl;
	
	return playerChar[0];

}

char playerNum;
playerNum = 1;

This code, is why it is not behaving as expected. You are assigned the char the numerical value 1, one, ie binary 00000001 as opposed to the letter that is '1'.

Try:

playerNum = '1';



Edit: emphasis on the single quotes around the 1.
Last edited on
I have tried it but it still does the same thing....
Initialize winner to 'N'.
That's it... thanks...
Topic archived. No new replies allowed.