Game in C++

Write your question here.

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  #include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>  //switch
#include <fstream>  //input output text file
using namespace std;

void new_board();
void rules();
void menu();





int main()
{
	char input;
	int answer;	
	cout << endl;

	menu();
	cin >> answer;
	
	if (answer == 1) // new game
	{
		system("cls");
		new_board();
	}
	else if (answer == 2) // rules
	{
		system("cls");
		rules();
		system("pause");
		main(); //start back from int main()
		cin >> answer;
		
	}
	else if (answer == 3)  // exit
	{
		cout << endl;
		cout <<"You've chose to exit. See you later!"<< endl;
		exit(1);
	}
	else
	{
		cout << endl;
		cout<<"Wrong Selection."<< endl;
		system("pause");
		main(); 
	}
	

	
	
	
	
	return 0;
}

void menu()
{
	system("cls");
	
	
	cout <<"----------------------------\n";
	cout <<"  Welcome to Othello game!\n";
	cout <<"----------------------------\n";
	cout <<"  1. New Game \n";
	cout <<"  2. Help \n";
	cout <<"  3. Quit \n";
	cout <<"----------------------------\n";
	cout <<"Enter your selection: ";
}

void rules()
{
	
	cout <<"-------------------------------------------------------------\n";
	cout <<"                Othello Game Rules\n";
	cout <<"-------------------------------------------------------------\n";
	cout <<"  1. Black always moves first \n";
	cout <<"  2. If you cannot outflank at least one opposing disc,\n"
	     <<"     your turn is forfeited and your opponent moves again.\n";
	cout <<"  3. A disc may outflank any discs in one or more rows\n"
		 <<"     in any directions at the same time.\n";
	cout <<"-------------------------------------------------------------\n";
	
	
}

void new_board()
{

	/*int Vertical = 8;
	int Horizontal = 8;
	char board[Vertical][Horizontal];*/
	int Vertical = 8;
	int Horizontal = 8;
	char board[8][8] = {{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},				//declaring and assigning for the boxes on the board
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
						{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}};

							//array for board
	
	
	for (int row = 0; row<Vertical; row++)
	{
		
			for (int col = 0; col <Horizontal; col++)
			{
				
				board[3][3] = 'X'; //4xD
				board[3][4] = 'O'; //4xE
				board[4][3] = 'O'; //5xD
				board[4][4] = 'X'; //5xE
			}
	}
			
		
		cout <<"  +---+---+---+---+---+---+---+---+"<< endl;
		
		for (int row = 0; row<Vertical; row++) //'<=' will output 1 extra box, '<' just nice. //build board
	{
		cout << row+1 <<" | ";
			for (int col = 0; col <Horizontal; col++)
			{
				cout << board[row][col] <<" | ";
			}
		cout << endl;
		cout <<"  +---+---+---+---+---+---+---+---+"<< endl;
	}	
	cout <<"    A   B   C   D   E   F   G   H" << endl;
	
	int Oscore = 0; ///SCORE FOR O
    int Xscore = 0; ///SCORE FOR X
     Oscore = Xscore = 0; ///INITIAL SCORE
	 cout <<endl;
	 
     for(int row = 0; row < Vertical; row++)
	 {
       for(int col = 0; col < Horizontal; col++)
       {
         Oscore += board[row][col] == 'O'; ///SCORE FOR O
         Xscore += board[row][col] == 'X'; ///SCORE FOR X
       }
	 }

     cout<<"     Score: O =  " << Oscore << endl;
	 cout<<"     Score: X =  " << Xscore; ///TOTAL POINTS FOR O AND X
	cout <<endl;
	
	///////////////////// new declaration ///////////////////
	bool player, cplayer, fplayer, splayer; // F: First, S: Second, C: Current
	string move; // string - problem (mingw), bool - problem(case '_'), char - cant compile
	// fplayer = false; 
	int inp, inpx;
	char inpy;
	
	do
	{
	
		cout <<"\n    ----------------------------\n";
		cout <<"\n     1. Next Player\n" // Enter 1: Current player - X (Swap with O)
			 <<"     2. Menu\n" // Proceed to menu
			 <<"     3. Enter Coordinates" // Enter 3: Current player - O
			 <<"\n\n     Input your selection: ";
			 cin >> inp;
			 cout <<"\n    ----------------------------\n";
			 
			 
		if (inp == 1)
		{
		   // current not needed. 
		   // If fplayer turn, then its false. 
		   // If its not, then it's true. 
		   if (cplayer = fplayer)
		   {
				cplayer = splayer;
		   }
		   else if (cplayer = splayer)
		   {
				cplayer = fplayer;
		   }
		}
		else if (inp == 2)
		{
			player = false; // break the loop and proceed to main.
			main();
		}
		else if (inp == 3) // <----- Problem starts here
		{
			cout << endl;
			cout <<"     Please enter a valid row followed by column (e.g. A5): ";
			cin >> move;
			cout <<"\n    ----------------------------\n";
			
			if((move != 'A') || (move != 'a') || (move != 'B') || (move != 'b') || 
			   (move != 'C') || (move != 'c') || (move != 'D') || (move != 'd') || 
			   (move != 'E') || (move != 'e') || (move != 'F') || (move != 'f') || 
			   (move != 'G') || (move != 'g') || (move != 'H') || (move != 'h'))
			   {
					cout <<"     Invalid input! Program ends.\n";
			   }
			 else
			   {	
				 switch(move)
				 {
					//case 'A':
					//case 'a':
					// start....
					case 'A':
					case 'a':
								move = 0;
								player = false;
								break;
								
					case 'B':
					case 'b':
								move = 1;
								player = false;
								break;
								
					case 'C':
					case 'c':
								move = 2;
								player = false;
								break;
								
					case 'D':
					case 'd':
								move = 3;
								player = false;
								break;

					case 'E':
					case 'e':
								move = 4;
								player = false;
								break;
								
					case 'F':
					case 'f':
								move = 5;
								player = false;
								break;
								
					case 'G':
					case 'g':
								move = 6;
								player = false;
								break;
								
					case 'H':
					case 'h':
								move = 7;
								player = true;
								break;
					
					default:
								cout << "Invalid Selection. Please try Again." << endl;
					
				 }
			   }
			
		}
		else
		{
			cout << "\n     Invalid move!\n";
			cout << endl;
			system("pause");
			main();
			
		}
			if (cplayer == fplayer) // refer to inp 1
			{
				cout <<"\tPlayer X: \n";
				board[inpx][inpy] = 'O';
				Oscore++; 
			}
			else // refer to inp 1
			{
				cout <<"Player O :\n";
				board[inpx][inpy] = 'X';
				Xscore++;
			}
			cout<<"     Score: O =  " << Oscore << endl;
			cout<<"     Score: X =  " << Xscore;
		
			 
	}while(player == true);
}	


The error message was something about mingw32. For 'move', string - problem (mingw), bool - problem(case ' '), char - cant compile. I'd very appreciated if any of you could help me.
Line 160: change the type from string to char.
I did tried to use char.. but then the compiler stopped responding..
That compiles [in the C++ shell]. What compiler do you use?


By the way: Note the assignment instead of comperison on line 182/186.
i use mingw32.. shud i use codeblock instead?
codeblock is an IDE that uses mingw. There is probably something wrong with the compiler hence I'd suggest that you download and install the latest mingw.

Using codeblocks is certainly ok.
ew, system("cls");
http://www.cplusplus.com/articles/j3wTURfi/

I have functions that I call to do this.
1
2
3
4
5
6
7
8
9
10
11
12
13
void clearScreen()
{
for (int i = 0; i < 100; i++)
{
std::cout << std::endl;
}
}


int main()
{
clearScreen();
}

There is a crazy effecient way of doing it that compresses it into one line, but i'm to lazy to remember it, and prefer to use a function. it's something like std::string(40, "\n");. I don't remember it, but it's something like that :P
Topic archived. No new replies allowed.