三子棋的简单实

简介: 三子棋的简单实

简单运用二维数组设置棋盘以及rand函数使电脑与玩家下棋

#include <iostream>
#include <time.h>
#include <stdlib.h>
#define ROW 3//设置行
#define LOW 3//设置列
using namespace std;
void game();
void Init(char board[ROW][LOW], int row, int low);//初始化,将棋盘赋值为空格
void Print(char board[ROW][LOW], int row, int low);//打印函数用于打印棋盘
char judgement(char board[ROW][LOW], int row, int low);//判断棋盘上是玩家赢输平
int main()
{
  srand((unsigned int)time(NULL));
  int input;
  while(1){
    for (int i = 0; i < 31; i++)
      cout << '*';
    cout << endl;
    for (int i = 0; i < 5; i++)
      cout << '*';
    cout << "Input 1 paly   0 exit";
    for (int i = 0; i < 5; i++)
      cout << '*';
    cout << endl;
    for (int i = 0; i < 31; i++)
      cout << '*';
    cout << endl;//打印菜单
    cout << "Input:";//用input判断开始游戏还是退出
    cin >> input;
    cout << endl;
    switch (input)
    {
    case 1:game();//游戏函数
      break;
    case 0:cout << "exit ";
      break;
    default:cout<<"input again";
    }
    if (!input)
      break;
  }
  return 0;
}
void Init(char board[ROW][LOW], int row, int low)
{
  for (int i = 0; i < row; i++)
    for (int j = 0; j < low; j++)
      board[i][j] = ' ';
}
void game()
{
  char board[ROW][LOW];
  Init(board, ROW, LOW);
  Print(board, ROW, LOW);
  while (1)
  {
    int a, b;
    cout << "player play and input coordinate: ";
    cin >> a >> b;
    cout << endl;
    if (a <= ROW && a >= 1 && b <= LOW && b >= 1)
    {
      if (board[a - 1][b - 1] != ' ')
      {
        cout << "coordinates are reentered in possession" << endl;;
        continue;
      }
      board[a - 1][b - 1] = '#';
      Print(board, ROW, LOW);
    }
    else
    {
      cout << "input error please re-type" << endl;
      continue;
    }
    cout << "computer play and input coordinate:";
    while (1)
    {
      int x = rand() % ROW + 1;
      int y = rand() % LOW + 1;
      if (board[x - 1][y - 1] != ' ')
        continue;
      cout << x<< ' ' << y << endl;
      board[x - 1][y - 1] = '*';
      Print(board, ROW, LOW);
      break;
    }
    char ch=judgement(board, ROW, LOW);
    if (ch == 'x')
      continue;
    if (ch == '#')
      cout << "player win";
    else if (ch == '*')
      cout << "computer win";
    else
      cout << "draw";
    cout << endl;
    break;
  }
}
void Print(char board[ROW][LOW], int row, int low)
{
  for (int i = 0; i < row; i++)
  {
    for (int j = 0; j < low; j++)
    {
      if (j)
        cout << '|';
      cout << ' '<<board[i][j]<<' ';
    }
    cout << endl;
    if (i != row - 1)
    {
      for (int j = 0; j < low; j++)
      {
        if (j)
          cout << '|';
        cout << "---";
      }
      cout << endl;
    }
  }
}
char judgement(char board[ROW][LOW], int row, int low)
{
  for (int i = 0; i < row; i++)//判断行
  {
    int flg = 0;
    for (int j = 1; j < low; j++)
      if (board[i][j] != board[i][j - 1])
      {
        flg = 1;
        break;
      }
    if (!flg && board[i][0] != ' ')
      return board[i][0];
  }
  for (int i = 0; i < row; i++)//判断列
  {
    int flg = 0;
    for (int j = 1; j < low; j++)
      if (board[j][i] != board[j - 1][i])
      {
        flg = 1;
        break;
      }
    if (!flg && board[0][i] != ' ')
      return board[0][i];
  }
  int flg = 0; int f = 0;//判断对角线
  for (int i = 0; i < row; i++)
  {if (board[i][i] != board[0][0])
      flg = 1;  
    if (board[row-1-i][i] != board[row-1][0])
      f = 1;
  }
  if (!flg && board[0][0] != ' ')
    return board[0][0];
  if (!f && board[row - 1][0] != ' ')
    return board[0][0];
  int vis = 0;
  for (int i = 0; i < row; i++)//判断棋盘是否下完
  {
    for (int j = 0; j < low; j++)
      if (board[i][j] == ' ')
      {
        vis = 1;
        break;
      }
    if (vis)
      break;
  }
  if (!vis)
    return 'p';
  return 'x';
}
目录
相关文章
|
7月前
|
C语言
简易三子棋的实现
简易三子棋的实现
三子棋的创建
三子棋的创建
85 0
简单三子棋的实现
简单三子棋的实现
66 0
|
程序员 C语言
三子棋(超详细)
三子棋(超详细)
|
C语言
【小朋友的三子棋】
【小朋友的三子棋】
73 0
|
C语言
C/【三子棋】
C/【三子棋】
三子棋的实现有这么简单吗?
三子棋的实现有这么简单吗?
54 0

热门文章

最新文章

相关实验场景

更多