Tic Tac Toe编程

时间:2014-05-14 05:41:08

标签: c++ tic-tac-toe

我是C ++编程的新手。我正在编写Tic Tac Toe游戏,以了解其算法的工作原理。我的问题是关于我有两个功能。其中一个是clearBoard函数,另一个是drawBoard函数。我对它们的帮助不大,但我无法弄清楚这些功能的工作方式。任何人都可以简单地解释一下究竟是什么?我一直试图解决它并理解它,但我认为我更加困惑。这可能是小菜一碟,但我真的想了解究竟发生了什么。感谢您的时间和关注......

这里的clearBoard功能:

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

这是我的drawBoard函数。

 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;
   }
  }

这是我的main.cpp文件。我只想发布我的整个作品至少会更容易看到完整的图片。

 #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;
  }

这是functions.cpp文件,它具有我上面讨论的功能。

#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;
 }

这是我的define.h头文件包含所有原型...

#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

1 个答案:

答案 0 :(得分:1)

理解你遇到困难的两个板功能的关键,clearBoard和drawBoard,你需要了解你存储游戏板的数据结构。事实上,这是一个非常简单的例子。您的电路板被定义为9个项目的数组:

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

考虑这一点的最佳方法是使用3 x 3 tic-tac-toe游戏板,并排放每一排。所以前三个元素是第一行,接下来的三个是第二行,后三个是最后一行。

drawBoard函数是两个函数中更复杂的函数,但是如果你理解了数组是什么以及它代表什么,它就会使这个函数更容易理解。外部for循环遍历游戏板的每个“行”,而内部for循环遍历该行中的每个元素。