Jump to content
  • Advertisement
Sign in to follow this  
Meta Adam

My Tic-Tac-Toe

This topic is 4929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there everyone. Here is my tic-tac-toe game. It is all finished, except the graphics and the AI, It wont work as of now because of both hehe. Take a look at my code and give me some feedback please.
#include <iostream>
//globals
bool playing;
bool myTurn;
bool placed;
int x, y, ox, oy; //ox and oy are where the opponent is placing a piece.
bool win, lose;
//cell class
class cell {
  public:
    enum token {EMPTY, X, O};
    token cellStatus;
    cell() : cellStatus(EMPTY) {};
};
//board class
class board {
  private:
    cell cells[3][3];
  public:
    int checkStatus(int x, int y);
    void setStatusX(int x, int y);
    void setStatusO(int x, int y);
    void printCannotPlace();
};
//class definitions
int board::checkStatus(int x, int y) {
  return cells[x][y].cellStatus;
}
void board::setStatusX(int x, int y) {
  if(cells[x][y].cellStatus == cell::EMPTY)
    cells[x][y].cellStatus = cell::X;
  else
    printCannotPlace();
}
void board::setStatusO(int x, int y) {
  if(cells[x][y].cellStatus == cell::EMPTY)
    cells[x][y].cellStatus = cell::O;
  else
    printCannotPlace();
}
void board::printCannotPlace() {
  std::cout<<"Cannot place a piece there."<<std::endl;
}
//function declarations
void gameLoop();
int checkGameStatus();
void getInput();
void opponentAI();
board myBoard;
int main() {
//notice how theres nothing here, poor main =( 
}
void getInput() {
  std::cout<<"Where do you want to place?  X/Y\n"
  std::cin>>x>>y;
}
void gameLoop() {
  playing = true;
  while(playing == true) {
    myTurn = true;
    while(myTurn == true) {
      getInput();
      myBoard.placeX(x, y);
      myTurn = false;
    }
    while(myTurn == false) {
      opponentAI();
      myBoard.placeO(ox, oy);
      myTurn = true;
    }
    checkGameStatus();
    if(win == true) {
      std::cout<<"You won! Good job!\n";
      playing = false;
    }
    if(lose == true) {
      std::cout<<"You lose =(. Too bad.\n";
      playing = false;
    }
  }
}
int checkGameStatus() {
  if(board.cells[0][0] == X && board.cells[0][1] == X && board.cells[0][2] == X)
    win = true;
  if(board.cells[0][0] == O && board.cells[0][1] == O && board.cells[0][2] == O)
    lose = true;
  if(board.cells[1][0] == X && board.cells[1][1] == X && board.cells[0][2] == X)
    win = true;
  if(board.cells[1][0] == O && board.cells[1][1] == O && board.cells[0][2] == O)
    lose = true;
  if(board.cells[2][0] == X && board.cells[2][1] == X && board.cells[2][2] == X)
    win = true;
  if(board.cells[2][0] == O && board.cells[2][1] == O && board.cells[2][2] == O)
    lose = true;
  if(board.cells[0][0] == X && board.cells[1][0] == X && board.cells[2][0] == X)
    win = true;
  if(board.cells[0][0] == O && board.cells[1][0] == O && board.cells[2][0] == O)
    lose = true;
  if(board.cells[0][1] == X && board.cells[1][1] == X && board.cells[2][1] == X)
    win = true;
  if(board.cells[0][1] == O && board.cells[1][1] == O && board.cells[2][1] == O)
    lose = true;
  if(board.cells[0][2] == X && board.cells[1][2] == X && board.cells[2][2] == X)
    win = true;
  if(board.cells[0][2] == O && board.cells[1][2] == O && board.cells[2][2] == O)
    lose = true;
  if(board.cells[0][0] == X && board.cells[1][1] == X && board.cells[2][2] == X)
    win = true;
  if(board.cells[0][0] == O && board.cells[1][1] == O && board.cells[2][2] == O)
    lose = true;
  if(board.cells[0][2] == X && board.cells[1][1] == X && board.cells[2][0] == X)
    win = true;
  if(board.cells[0][2] == O && board.cells[1][1] == O && board.cells[2][0] == O)
    lose = true;
  else
    win = false;
    lose = false;
}

Share this post


Link to post
Share on other sites
Advertisement
while(myTurn == true) {
getInput();
myBoard.placeX(x, y);
myTurn = false;
}
while(myTurn == false) {
opponentAI();
myBoard.placeO(ox, oy);
myTurn = true;
}


The above while loops are uneeded. They will never loop.
// my turn
getInput();
myBoard.placeX(x, y);

//opponents turn
opponentAI();
myBoard.placeO(ox, oy);

would work the same way and save you some code.

Share this post


Link to post
Share on other sites
In your board checking routine:

if(board.cells[0][0] == X && board.cells[0][1] == X && board.cells[0][2] == X)
win = true;
if(board.cells[0][0] == O && board.cells[0][1] == O && board.cells[0][2] == O)
lose = true;
if(board.cells[1][0] == X && board.cells[1][1] == X && board.cells[0][2] == X)
win = true;
if(board.cells[1][0] == O && board.cells[1][1] == O && board.cells[0][2] == O)
lose = true;

You check the entire board, even if you found a winner. You should return from this function as soon as a winner is found.

Share this post


Link to post
Share on other sites
In your class:

class cell {
public:
enum token {EMPTY, X, O};
token cellStatus;
cell() : cellStatus(EMPTY) {};
};


You have no private members. You are defeating the purpose of a class. Make this a struct or give it some sort of accessor methods. This is more of a syntactical thing really.

Share this post


Link to post
Share on other sites
Quote:
can I write a constructor in a struct? I never use them...

Absolutely, structs and classes are the same animal, but classes default to private and structs default to public access.


Quote:
so return 1 and it will go out of the function right?

return 1 when you detect a win and 0 when you detect a loss.


Sorry if I seem overly critical, I don't mean to be. Just trying to give you some feedback :)

Share this post


Link to post
Share on other sites
Aha, now I remember why I had checkstatus LOL. In my checkboard functions, I noticed I'm trying to access cell cells[3][3] directly lol. Ill have to fix that.

Share this post


Link to post
Share on other sites
Updated Code: Still without graphics or AI.

#include <iostream>
//globals
bool playing;
int x, y, ox, oy;
bool win, lose;
//cell class
class cell {
public:
enum token {EMPTY, X, O};
token cellStatus;
cell() : cellStatus(EMPTY) {};
};
//board class
class board {
private:
cell cells[3][3];
public:
int checkStatus(int x, int y);
void setStatusX(int x, int y);
void setStatusO(int x, int y);
void printCannotPlace();
};
//class definitions
int board::checkStatus(int x, int y) {
return cells[x][y].cellStatus;
}
void board::setStatusX(int x, int y) {
if(cells[x][y].cellStatus == cell::EMPTY)
cells[x][y].cellStatus = cell::X;
else
printCannotPlace();
}
void board::setStatusO(int x, int y) {
if(cells[x][y].cellStatus == cell::EMPTY)
cells[x][y].cellStatus = cell::O;
else
printCannotPlace();
}
void board::printCannotPlace() {
std::cout<<"Cannot place a piece there."<<std::endl;
}
//function declarations
void gameLoop();
int checkGameStatus();
void getInput();
void opponentAI();
board myBoard;
int main() {

}
void getInput() {
std::cout<<"Where do you want to place? X/Y\n";
std::cin>>x>>y;
}
void gameLoop() {
playing = true;
while(playing == true) {
getInput();
myBoard.setStatusX(x, y);
myTurn = false;
opponentAI();
myBoard.setStatusO(ox, oy);
checkGameStatus();
if(win == true) {
std::cout<<"You won! Good job!\n";
playing = false;
}
if(lose == true) {
std::cout<<"You lose =(. Too bad.\n";
playing = false;
}
}
}
int checkGameStatus() {
if(myBoard.checkStatus(0, 0) == cell::X && myBoard.checkStatus(0, 1) == cell::X && myBoard.checkStatus(0, 2) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(0, 0) == cell::O && myBoard.checkStatus(0, 1) == cell::O && myBoard.checkStatus(0, 2) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(1, 0) == cell::X && myBoard.checkStatus(1, 1) == cell::X && myBoard.checkStatus(0, 2) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(1, 0) == cell::O && myBoard.checkStatus(1, 1) == cell::O && myBoard.checkStatus(0, 2) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(2, 0) == cell::X && myBoard.checkStatus(2, 1) == cell::X && myBoard.checkStatus(2, 2) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(2, 0) == cell::O && myBoard.checkStatus(2, 1) == cell::O && myBoard.checkStatus(2, 2) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(0, 0) == cell::X && myBoard.checkStatus(1, 0) == cell::X && myBoard.checkStatus(2, 0) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(0, 0) == cell::O && myBoard.checkStatus(1, 0) == cell::O && myBoard.checkStatus(2, 0) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(0, 1) == cell::X && myBoard.checkStatus(1, 1) == cell::X && myBoard.checkStatus(2, 1) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(0, 1) == cell::O && myBoard.checkStatus(1, 1) == cell::O && myBoard.checkStatus(2, 1) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(0, 2) == cell::X && myBoard.checkStatus(1, 2) == cell::X && myBoard.checkStatus(2, 2) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(0, 2) == cell::O && myBoard.checkStatus(1, 2) == cell::O && myBoard.checkStatus(2, 2) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(0, 0) == cell::X && myBoard.checkStatus(1, 1) == cell::X && myBoard.checkStatus(2, 2) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(0, 0) == cell::O && myBoard.checkStatus(1, 1) == cell::O && myBoard.checkStatus(2, 2) == cell::O) {
lose = true;
return 0; }
if(myBoard.checkStatus(0, 2) == cell::X && myBoard.checkStatus(1, 1) == cell::X && myBoard.checkStatus(2, 0) == cell::X) {
win = true;
return 1; }
if(myBoard.checkStatus(0, 2) == cell::O && myBoard.checkStatus(1, 1) == cell::O && myBoard.checkStatus(2, 0) == cell::O) {
lose = true;
return 0; }
else {
win = false;
lose = false; }
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!