?
Solved

Programming Tic Tac toe in C++

Posted on 2003-03-17
5
Medium Priority
?
1,301 Views
Last Modified: 2008-03-10
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
Comment
Question by:Camaro67Racer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
TrueIdiot earned 300 total points
ID: 8156243
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
 
LVL 2

Expert Comment

by:Elias Saliba
ID: 8159041
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
 
LVL 2

Expert Comment

by:Elias Saliba
ID: 8159042
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
 
LVL 1

Expert Comment

by:TrueIdiot
ID: 8163438
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9502369
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question