• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1330
  • Last Modified:

Programming Tic Tac toe in C++

Please help, I need to know where to go in this program. I am very lost, the descriptions of what needs to be done in each function are above it, I just don't really know how to go about doing it, Please help, thanks!!


#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void lineScore (vector <vector <int> > board, vector <int> & lineNumber);
void nextMove (vector <vector <int> > board, int & iMove, int & jMove);
void printBoard(ostream & out, vector<vector<int> > board);

void playGame(vector<vector<int> > board, bool & xWon, bool & oWon);
void findBlank (vector <vector <int> > board, int which, int & iMove, int & jMove);
int countMoves (vector <vector <int> > board);

int main () {
  vector<int> zeros(3,0);
  vector<vector<int> >board(3,zeros);
  bool xWon, oWon;

  playGame(board,xWon,oWon);

  cout<<"xWon = "<<xWon<<"  oWon = "<<oWon<<endl;
return(0);
}

void lineScore (vector <vector <int> > board, vector <int> & lineScores){

 
/**************************************************************************************  
    Input: tic tac toe board
   Output: The coordinates where the computer is moving
   Descri: This prodedure will follow the basic strategy:
            1. Move to win
            2. Move to block
            3. Play out the game when nobody will win or lose.
         Advanced strategy will user specific depending the strategy that you use.
**************************************************************************************/
void nextMove (vector <vector <int> > board, int & iMove, int & jMove){






}//end of nextMove procedure



/**************************************************************************************  
    Input: tic tac toe board, which
   Output: a blank space at coordinates iMove, jMove
   Descri: Finds a blank space in a specific line location.  Thus the variable 'which'
         corresponds to which of the seven (7) possibilities you can have to win.
**************************************************************************************/
void findBlank (vector <vector <int> > board, int which, int & iMove, int & jMove) {






} // end of findBlank procedure


/**************************************************************************************  
    Input: tic tac toe board
   Output: returns the number of moves
   Descri: Determines the number of moves made so far.  So on a blank board, the
         number of moves made so far is 0. This function will return that value.
**************************************************************************************/
int countMoves (vector <vector <int> > board) {
  int moves=0;
  for (int i=0; i<board.size(); i++) for (int j=0; j<board[0].size();j++)
    if (board[i][j]!=0) moves++;
return(moves);
} // end of countMoves function

void printBoard(ostream & out, vector<vector<int> > board) {
  for (int i=0; i<board.size();i++){ cout<<" ";
    for (int j=0; j<board[0].size(); j++) {
      if (board[i][j]==-1) {out<<" O ";}
      else if (board[i][j]==1) {out<<" X ";}
      else {out<<"   ";}
      if (j!=2) {out<<" | ";}
    }
    if (i!=2) {out<<"\n-----+-----+-----\n";}
  } cout<<endl;
} // end of printBoard

/**************************************************************************************  
    Input: tic tac toe board
   Output: The coordinates where the human is moving
   Descri: With board as input, the procedure will ask the human where s/he wants to
         move.  It then checks, in the while to see if the move is legal...legal
         being that it is not occupied or not.  It does not check if you've moved
         outside the board.  It assumes you will move 0<=i<=2, 0<=j<=2.
**************************************************************************************/
void humanMove(vector<vector<int> > board, int & iMove, int & jMove) {
  cout<<"\nEnter Move (row  col): "; cin>>iMove>>jMove;
  while (board[iMove][jMove] !=0) {      // checks if space is occupied or not
    cout<<"\nIllegal Move("<<iMove<<" "<<jMove<<"), re-enter: "; cin>>iMove>>jMove;
  }
} // end of humanMove procedure



/**************************************************************************************  
    Input: tic tac toe board
   Output: Boolean variables xWon and oWon.  Tells the user who won/lost/tie
   Descri: Plays the game until the board is full or if anybody won.  This procedure
         also keeps tracks of whose turn is it to move.
**************************************************************************************/
void playGame(vector<vector<int> > board, bool & xWon, bool & oWon){
















} // end of playGame


0
Camaro67Racer
Asked:
Camaro67Racer
  • 2
  • 2
1 Solution
 
TrueIdiotCommented:
You're trying too hard. You can do it with a lot less functions, if you declare board as a global, and if you use a character to determine who won (matrices are better than doulbed vectors too):

#include<iostream>
#include<matrix>

using namespace std;

void printBoard();
char playGame();
void compGo();
void playerGo();
char winCheck();

matrix<char> board(3,3);

int main()
{
   char winner;
   
   for(int x = 0;x < 3;x++)  //used to initalize matrix
   {
      for(int y = 0;y < 3;y++)
      {
         board(i,y) = i + y + 1; //fills board with values from 1 to 9
      }
   }
   
   winner = playGame();

   cout << "/n/nThe winner is " << winner
}//end of intmain

in the play game function use an do while loop with an incrementing integer that calls first printBoard(), then playerGo(), then winCheck. After wincheck() have an if break statement (if wincheck =  o || x then break), then call compGo(), winCheck(), if break and then the end of the loop. The loop should go until the number of iterations has reached 5, then quit with a draw or winner.


the player go should take in a number (corresponding the board) and convert it into x and y coordinates (i.e. 1 = 0,0, 2 = 0,1 and so forth), make sure that spot is free, then slap down that player's mark or ask him to go again


the compGo is the hard part. This is where the AI comes in. The key to writing good AI is to write some basic AI (like try to take the center first and so on) and then play the comp. When you beat it, think about what it did wrong, then write code to correct it (i would give you the code for winning moves and blocking, but I'm tired. When i log on tomorrow, maybe)


the print board uses two nested for loops (like in the board initialization), but this time couts every character, then when the inner for loop ends, cout an endl.

the win chech is actually pretty easy. First, declare 2 intigers for Xs and Os. Then check rows and columns, and for every x, increment the X counter, for every O, increment the o counter. Diagonals are a little trickier, but I'm too tired right now.


Please post a comment or two on whether or not you want me to keep going tomorrow. I don't want to do more work than I have to :)
0
 
Elias SalibaCommented:
as mentionned by  TrueIdiot  it is very difficulte to do it using vectors....
simple function and a double dim. array, will do the work for ya:
first  put all win condition in an array i.e:

const int Three_in_a_Row[8][3] = {
    { 0, 1, 2 },
    { 3, 4, 5 },
    { 6, 7, 8 },
    { 0, 3, 6 },
    { 1, 4, 7 },
    { 2, 5, 8 },
    { 0, 4, 8 },
    { 2, 4, 6 }
};
considering the following possible moves:
 1 | 2 | 3    //the position 1 is zero in the 2x2 array and
---+---+---   //so on
 4 | 5 | 6
---+---+---
 7 | 8 | 9
and so on ...
0
 
Elias SalibaCommented:
as mentionned by  TrueIdiot  it is very difficulte to do it using vectors....
simple function and a double dim. array, will do the work for ya:
first  put all win condition in an array i.e:

const int Three_in_a_Row[8][3] = {
    { 0, 1, 2 },
    { 3, 4, 5 },
    { 6, 7, 8 },
    { 0, 3, 6 },
    { 1, 4, 7 },
    { 2, 5, 8 },
    { 0, 4, 8 },
    { 2, 4, 6 }
};
considering the following possible moves:
 1 | 2 | 3    //the position 1 is zero in the 2x2 array and
---+---+---   //so on
 4 | 5 | 6
---+---+---
 7 | 8 | 9
and so on ...
0
 
TrueIdiotCommented:
also, nested for loops work just as well:

char winCheck()
{
   int X=0;
   int O=0;

   for(int x = 0;x < 3; x++) //this checks columns
   {
      for(int y = 0;y < 3; y++)
      {
         if board[x][y] = 'o' //make sure all entries you input into the board
            O++              //(in your code) are in
lowercase
         if board[x][y] = 'x' //p.s. make sure to acess the array this way
            X++
       }
    }

   for(int y = 0;y < 3; y++) //this checks rows
   {
      for(int x = 0;x < 3; x++)
      {
         if board[x][y] = 'o'
            O++
         if board[x][y] = 'x'
            X++
       }
    }
           
   for(int i=0;i<3;i++)
   {
       if board[i][i] = 'o'
          O++
       if board[i][i] = 'x'
          X++
   }

sorry, but i forgot how to do the diagonal the other way <slams head on desk> I think it's a nested for loop with x incrementing and y decrementing or somesuch folly

also, if you need any help, I'm still here all week (actually, I'll probably be here till either I die, or EE closes down)
                                c ya

P.S. in my posted previously posted int main() I realized the initialization loop should use x instead of i

P.P.S. Isn't the tab key not working so annoying?
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: TrueIdiot

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now