Another Tic Tac Toe

This is a discussion on Another Tic Tac Toe within the C++ Programming forums, part of the General Programming Boards category; Well after my exams I have had trouble getting back into C++ so I thought I would start with something ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Another Tic Tac Toe

    Well after my exams I have had trouble getting back into C++ so I thought I would start with something easy like Tic Tac Toe.

    Header File:
    Code:
    void drawboard () {
    cout << board[0][0] << '|' << board[0][1] << '|' << board[0][2] << endl;
    cout << "-----" << endl;
    cout << board[1][0] << '|' << board[1][1] << '|' << board[1][2] << endl;
    cout << "-----" << endl;
    cout << board[2][0] << '|' << board[2][1] << '|' << board[2][2] << endl;
    cout << endl;
    }
    
    char turn () {
         int temp = pturn % 2;
         char player;
              if (temp != 0) {
              player = 'O';
              }
              else {
              player = 'X';
              }
    return  player;
    }
    
    
    
    void move () {
    
    int row;
    int col;
    
    do {
       cout << "Player " << pchar << "'s turn" << endl << endl;
       cout << "Please enter the row number: ";
       cin >> row;
       cout << "Please enter the column number: ";
       cin >> col;
       --row;
       --col;
       if (!usedsq[row][col]) {
          break;
       }
    cout << endl << "Sorry that square is taken" << endl;
    cin.ignore();
    cin.get();
    system("CLS");
    drawboard();
    } while (1);
    
    board[row][col] = pchar;
    usedsq[row][col] = true;
    system("CLS");
    
    }
    
    void win() {
    
    if (board[0][0] == 'X' &&  board[0][1] == 'X' &&
        board[0][2] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[1][0] == 'X' &&  board[1][1] == 'X' &&
        board[1][2] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[2][0] == 'X' &&  board[2][1] == 'X' &&
        board[2][2] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][0] == 'X' &&  board[1][1] == 'X' &&
        board[2][2] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][2] == 'X' &&  board[1][1] == 'X' &&
        board[2][0] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][0] == 'X' &&  board[1][0] == 'X' &&
        board[2][0] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][1] == 'X' &&  board[1][1] == 'X' &&
        board[2][1] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][2] == 'X' &&  board[1][2] == 'X' &&
        board[2][2] == 'X') {
    
        cout << "Congratulations Player X has won!" << endl;
        pwin = true;
        }
    
    if (board[0][0] == 'O' &&  board[0][1] == 'O' &&
        board[0][2] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[1][0] == 'O' &&  board[1][1] == 'O' &&
        board[1][2] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[2][0] == 'O' &&  board[2][1] == 'O' &&
        board[2][2] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][0] == 'O' &&  board[1][1] == 'O' &&
        board[2][2] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][2] == 'O' &&  board[1][1] == 'O' &&
        board[2][0] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][0] == 'O' &&  board[1][0] == 'O' &&
        board[2][0] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][1] == 'O' &&  board[1][1] == 'O' &&
        board[2][1] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    
    else if (board[0][2] == 'O' &&  board[1][2] == 'O' &&
        board[2][2] == 'O') {
    
        cout << "Congratulations Player O has won!" << endl;
        pwin = true;
        }
    }
    *That is a very long conditional statement I have there, wasn't sure how to cut it down*

    Main:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include "TicHead.h"
    
    using namespace std;
    
    int main()
    {
          int menuselect;
          char end = 'N';
          int noturns = 0;
    
          cout << endl << "Welcome to Bumfluff's Tic Tac Toe" << endl;
          cout << endl << "\t1. Start Game" << endl;
          cout << "\t2. Instructions" << endl;
          cout << "\t3. Quit" << endl;
          cout << endl << "Please make a selection: ";
          cin >> menuselect;
          system("CLS");
    
          switch (menuselect) {
                 case 1:
                      break;
                 case 2:
                      cout << "This is a two player game." << endl;
                      cout << "Type the number of the square" <<endl;
                      cout << "and press enter to put your character" << endl;
                      cout << "into your chosen square. You choose" << endl;
                      cout << "the square by selecting the column and row." << endl;
                      cout << endl << "The  first person to get three in a row WINS!" << endl;
                      cout << endl << "Press [ENTER] To Start Game: ";
                      cin.ignore();
                      cin.get();
                      break;
                case 3:
                     return 0;
                default:
                     return -1;
          }
    
    do {
          pturn = 0;
          noturns = 0;
          do {
          system("CLS");
          drawboard();
          pchar = turn();
          move();
          win();
          if (pwin) break;
          ++pturn;
          if (noturns == 8) {
          cout << "You have drawn" << endl;
          break;
          }
          ++noturns;
          } while (1);
    
          cout << endl << "Would you like to play again? (Y/N): ";
          cin >> end;
          if (end == 'N' || end == 'n') break;
    
          for (int i = 0; i < 3; i++) {
              for (int j = 0; j < 3;  j++){
                  board[i][j] = 0;
                  usedsq[i][j] = 0;
              }
          }
    } while (1);
    
          return 0;
    }
    *I know I have used system commands*

    My question is how would I go about making this a 1 player vs. computer game ie. how I make the algorithms.
    Last edited by bumfluff; 06-18-2006 at 05:18 AM.

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    This isn't really answering your question but check out this other way of drawing any 2-dim board

    Code:
    void drawboard () {
    cout << board[0][0] << '|' << board[0][1] << '|' << board[0][2] << endl;
    cout << "-----" << endl;
    cout << board[1][0] << '|' << board[1][1] << '|' << board[1][2] << endl;
    cout << "-----" << endl;
    cout << board[2][0] << '|' << board[2][1] << '|' << board[2][2] << endl;
    cout << endl;
    }
    Imagine coding the output for even a 10x10 grid.

    Here's a better way:
    Code:
    void drawboard()
    {
          for (int row = 0;  row < NUM_ROWS; row++)
          {
                for (int col = 0; col < NUM_COLS; col++)
                       cout << board[row][col];
                cout << endl;
          }
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yeah I can see that, but then again that doesnt use the symbold in between, but for any other grid that would be useful.

    I am jsut about to edit the code so it works properly.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    The game actually has a few bugs as I realised that you can't draw or play again...solving them.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Solved, they were very very minor changes.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Now, to the nitty gritty, creating a computer player.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    For an idea on how to implement a tic-tac-toe AI
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Thanks that is really useful, could I make 8 structures of every line or something and in that stores each location of that line and works out the priority of that line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 07:25 PM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 07:25 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 09:33 AM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 05:21 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21