Question

I am pretty new in C++ programming. I am coding for Tic Tac Toe game to understand how its algorithm works. My question is about I have two functions. One of them is clearBoard function and the other one is drawBoard function. I got little help about them but I couldnt figure it out the way those functions works. Can anyone simply explain me what is exactly what? I have been trying to solve it and understand it but I think I got more confused. It might be a piece of cake but I really want to understand what's exactly going on. Thanks for your time and attention...

Here clearBoard function:

void clearBoard(int board[])
{
 int i;
  for(i = 0; i < board_size; i++) {
   board[i] = -i - 1;
 }
}

And here is my drawBoard function.

 void drawBoard(int board[])
 {
  int i, j;
  int n = 0;
  for(i = 0; i <= 6; i = i+3) {
   for(j = 0; j < 3; ++j) {
    if(board[i + j] == 2)
    cout << "|X|";
     else if(board[i + j] == 1)
    cout << "|O|";
     else
    cout << "|" << n << "|";
     n = n+1;
    }
   cout << endl;
   }
  }

This is my main.cpp file. I just wanted to post my entire work at least it will be easier to see complete picture.

 #include <iostream>
 #include <ctime>
 #include<cstdlib>
 #include "Define.h"
 using namespace std;


 int main()
 {
  int board[board_size];
  int turn = 0;
  int p = 0;
  int move = 10;
  srand(time(NULL));
  clearBoard(board);


  cout << "This turn is randomly chosen!" << endl;
  p = random(2) + 1;
  cout << "The first move goes to Player: " << p << endl;
  turn = p;

  do {
   if(p== 2 && 2 == turn)
  {
    drawBoard(board);
    move = getPlayerMove(turn);
  }
  else
    move = makeRandMove(turn);
  } while(!isMoveValid(board, move));
    board[move] = turn;

 while(!isWin(board, move)){
 drawBoard(board);
  if(2 == turn)
    turn = 1;
  else
    turn = 2;
  do {
   if(2 == turn)
      move = getPlayerMove(turn);
  else
    move = makeRandMove(turn);
  } while(!isMoveValid(board, move));
   board[move] = turn;
  }
  drawBoard(board);
  cout << "Player " << turn << " wins." << endl;
  return 0;
  }

And this is the functions.cpp file that has the functions that I was talking about above.

#include <iostream>
#include<cstdlib>
using namespace std;


const int board_size = 9;

void clearBoard(int board[])
{
 int i;
  for(i = 0; i < board_size; i++) {
  board[i] = -i - 1;
 }
 }

 int random(int x)
 {
 return rand() % x;
 }

 void drawBoard(int board[])
 {
 int i, j;
 int n = 0;
 for(i = 0; i <= 6; i = i+3) {
  for(j = 0; j < 3; ++j) {
    if(board[i + j] == 2)
 cout << "|X|";
  else if(board[i + j] == 1)
cout << "|O|";
  else
cout << "|" << n << "|";
n = n+1;
}
cout << endl;
 }
 }


int getPlayerMove(int player)
{
int move;
cout << "Player " << player << " enter move: ";
cin >> move;
return move;
}


int makeRandMove(int player)
{
cout << "Computer (player " << player << ") moving." << endl;
return rand() % board_size;
}


 bool isMoveValid(int board[], int move)
 {
  if(board[move] < 0)
  return true;
  return false;
  }


  bool isWin(int board[], int move)
  {
  if((board[0] == board[1] && board[0] == board[2]) ||
 (board[3] == board[4] && board[3] == board[5]) ||
 (board[6] == board[7] && board[6] == board[8]) ||
 (board[0] == board[3] && board[0] == board[6]) ||
 (board[1] == board[4] && board[1] == board[7]) ||
 (board[2] == board[5] && board[2] == board[8]) ||
 (board[0] == board[4] && board[0] == board[8]) ||
 (board[2] == board[4] && board[2] == board[6]))
  return true;
  return false;
 }

And here is my define.h header file has all the protypes...

#ifndef formula
#define formula

const int board_size = 9;
int random(int x);
void clearBoard(int board[]);
void drawBoard(int board[]);
int getPlayerMove(int player);
int makeRandMove(int player);
bool isMoveValid(int boardp[], int move);
bool isWin(int board[], int move);

#endif
Was it helpful?

Solution

The key to understanding the two board functions you are having difficulty with, clearBoard and drawBoard, you need to understand the data structure that you're storing your game board in. This is, in fact, a pretty simple example. Your board is defined as an array of 9 items:

int board[board_size];
...
const int board_size = 9;

The best way to think about this is if you take your 3 x 3 tic-tac-toe game board, and lay each row side by side. So the first three elements are the first row, the next three are the second row, and the last three are the final row.

The drawBoard function is the more complex of the two functions, but if you understand what the array is and what it represents, it makes this function much more understandable. The outer for loop is looping through each "row" of the gameboard, while the inner for loop loops through each element in that row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top