# Jumping Into C++ - Chapter 8 Problem[3]

This is a discussion on Jumping Into C++ - Chapter 8 Problem[3] within the C++ Programming forums, part of the General Programming Boards category; Hello I Am C++ Beginner Learning C++ From Jumping Into C++ Book I Am In Chapter 8 Practice Problem 3 ...

1. ## Jumping Into C++ - Chapter 8 Problem[3]

Hello I Am C++ Beginner Learning C++
From Jumping Into C++ Book

I Am In Chapter 8 Practice Problem 3 It Say :
Write a two-player tic-tac-toe game; use enums when possible to represent the values of the board

I Want To Know If My Code Is Right [I Guess Not :/]
Here Is My Code :
Code:
```// Chapter 8 - Practical Problems[3]
#include <iostream>

using namespace std;
// Variables Declarations
enum moves {blank_square , X, O};
moves choose_move = X;
char ph1 = '1', ph2 = '2', ph3 = '3', ph4 = '4', ph5 = '5', ph6 = '6', ph7 = '7', ph8 = '8', ph9 = '9';
char x;
void board (){ // New Function For Tic_Tac_Toe Game Filed
//Board Interface - Starts Here
cout << ph1 << " | " << ph2 << " | " << ph3 << endl;
cout << "--*" << "---*" << "--" << endl;
cout << ph4 << " | " << ph5 << " | " << ph6 << endl;
cout << "--*" << "---*" << "--" << endl;
cout << ph7 << " | " << ph8 << " | " << ph9 << endl;
}
void player_turn(){ // New Function To Detrmine Whiche Players Turn
switch (choose_move){
case X : // Case choose_move == X -> Do Some Code Here
cout << "\n[X]Turn -> Choose A Number :"; // [Output]
x = 'X'; // make The x Variable = 'X'
choose_move = O;
break; // Break To Prevent Falling Throug Cases
case O : // Case choose_move == O -> Do Some Code Here
cout << "\n[O]Turn -> Choose A Number :"; // [Output]
x = 'O'; // make The x Variable = 'O'
choose_move = X;
break;
}
}
void X_O_Moves(){ // New Function To Make Moves
int i;
cin >> i; // [input] Number
switch(i){
case 1 : // Case i Equal 1 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph1 == 'X' || ph1 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph1 = x;break;
case 2 : // Case i Equal 2 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph2 == 'X' || ph2 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph2 = x;break;
case 3 : // Case i Equal 3 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph3 == 'X' || ph3 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph3 = x;break;
case 4 : // Case i Equal 4 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph4 == 'X' || ph4 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph4 = x;break;
case 5 : // Case i Equal 5 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph5 == 'X' || ph5 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";;X_O_Moves();break;}
ph5 = x;break;
case 6 : // Case i Equal 6 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph6 == 'X' || ph6 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";;X_O_Moves();break;}
ph6 = x;break;
case 7 : // Case i Equal 7 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph7 == 'X' || ph7 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph7 = x;break;
case 8 : // Case i Equal 8 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph8 == 'X' || ph8 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph8 = x;break;
case 9 : // Case i Equal 9 -> Check If The Fild Is Already Taken -> filed = 'X' If It isnot Taken
if (ph9 == 'X' || ph9 == 'O'){cout << "This Fild Is Already Taken , Try Again : ";X_O_Moves();break;}
ph9 = x;break;
}
}
// New Boolean Function To Check For The Winning Moves If There Any winning Moves Return True Else Return False
bool check_moves(){
// Check ---*---*---
if (ph1 == x && ph2 == x && ph3 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
else if (ph4 == x && ph5 == x && ph6 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
else if (ph7 == x && ph8 == x && ph9 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
// Check ---|---|---
else if (ph1 == x && ph4 == x && ph7 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
else if (ph2 == x && ph5 == x && ph8 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
else if (ph3 == x && ph6 == x && ph9 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
// Check \ Or /
else if (ph1 == x && ph5 == x && ph9 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
else if (ph3 == x && ph5 == x && ph7 == x){cout << x << " Won , Congratulations ^_^" << endl;return true;}
return false; // Return False By Default
}
// Main Function
int main ()
{   // Interface
cout << "======[Simple Multi-Player [Tic-Tac_Toe] Game]======\n";
cout << "Player One = [X]\n";
cout << "Player Two = [O]\n";
cout << endl;
while(true){ // Looop Until It Breaks
board(); // Call Board Function
if (check_moves()){break;} // Check If check_moves Is True Or False [If True Break The Loop]
else { // If check_moves = false Then Excute The Below Codes
player_turn();
X_O_Moves();
}
}
// Extra Code For Pasuing The Program
cin.ignore();
cin.get();
}

```
Thank You

2. Nice post.

I suggest you test your code and then explain why you think it's not ok. Does it compile? Does it give output you do not expect? Does it crash?

3. whiteflags' Picture Guide to Using an IDE for the Lost:

1. Copy

2. Paste into editor

3. Click a build button somewhere, like this one.

4. Check yourself before you wreck yourself.

By the way, apart from the warnings, it does play tic tac toe. I didn't spend to much time with it though.

The point is you can do this yourself, and this kind of "what's wrong" question is very unattractive. Please be more specific with your problems in the future. You can utilize us more than you actually are.

4. Originally Posted by whiteflags
whiteflags' Picture Guide to Using an IDE for the Lost:

1. Copy

2. Paste into editor

3. Click a build button somewhere, like this one.

4. Check yourself before you wreck yourself.

By the way, apart from the warnings, it does play tic tac toe. I didn't spend to much time with it though.

The point is you can do this yourself, and this kind of "what's wrong" question is very unattractive. Please be more specific with your problems in the future. You can utilize us more than you actually are.
Okay I Will
Thank You Very Much ^_^