Thread: Jumping Into C++ - Chapter 8 Problem[3]

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3

    Question 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
    Last edited by Mohamed Adel; 08-28-2013 at 08:28 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    whiteflags' Picture Guide to Using an IDE for the Lost:

    This is exactly what I did to answer your "question."

    1. Copy
    Jumping Into C++ - Chapter 8 Problem[3]-copy-jpg

    2. Paste into editor
    Jumping Into C++ - Chapter 8 Problem[3]-paste-jpg

    3. Click a build button somewhere, like this one.
    Jumping Into C++ - Chapter 8 Problem[3]-run-jpg

    4. Check yourself before you wreck yourself.
    Jumping Into C++ - Chapter 8 Problem[3]-check-yourself-b4-u-wreck-yourself-jpg

    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. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    whiteflags' Picture Guide to Using an IDE for the Lost:

    This is exactly what I did to answer your "question."

    1. Copy
    Jumping Into C++ - Chapter 8 Problem[3]-copy-jpg

    2. Paste into editor
    Jumping Into C++ - Chapter 8 Problem[3]-paste-jpg

    3. Click a build button somewhere, like this one.
    Jumping Into C++ - Chapter 8 Problem[3]-run-jpg

    4. Check yourself before you wreck yourself.
    Jumping Into C++ - Chapter 8 Problem[3]-check-yourself-b4-u-wreck-yourself-jpg

    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 ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  2. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM
  3. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  4. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 05:44 PM
  5. problem with quix chapter 4
    By roelof in forum C++ Programming
    Replies: 2
    Last Post: 05-31-2010, 08:38 AM

Tags for this Thread