天天看點

雙人三子棋

.c源檔案

#define _CRT_SECURE_NO_WARNINGS
#include"game.h"

void game()
{
  char ret = 0;
  char board[ROW][COL] = { 0 };
  initboard(board, ROW, COL);//建立函數初始化棋盤,全為空格
  //列印棋盤
  displayboard(board, ROW, COL);
  //下棋
  while (1)
  {
    //玩家1下棋
    player1move(board, ROW, COL);
    displayboard(board, ROW, COL);
    //判斷輸赢
    ret = iswin(board, ROW, COL);
    if (ret != 'C')
    {
      break;
    }
    //玩家2下棋
    player2move(board, ROW, COL);
    displayboard(board, ROW, COL);
    ret = iswin(board, ROW, COL);
    if (ret != 'C')
    {
      break;
    }
  }
  if (ret == '*')
  {
    printf("恭喜玩家1勝利!\n");
  }
  else if (ret == '#')
  {
    printf("恭喜玩家2勝利!\n");
  }
  else
  {
    printf("平局。\n");
  }
}

void menu()
{
  printf("\n");
  printf("       1.play         0.exit       \n");
  printf("\n");
}

void test()
{
  int input = 0;
  srand((unsigned)time(NULL));
  do {
    menu();
    printf("請選擇:>");
    scanf("%d", &input);
    switch (input)
    {
    case 1:
      printf("開始遊戲,三子棋。\n");
      game();
      break;
    case 0:
      printf("退出遊戲。\n");
      break;
    default:
      printf("輸入錯誤,請重試。\n");
      break;
    }
  } while (input);
}

int main()
{
  test();
  return 0;
}      
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

void initboard(char board[ROW][COL], int row, int col)
{
  int i = 0;
  int j = 0;
  for (i = 0; i < row; i++)
  {
    for (j = 0; j < col; j++)
    {
      board[i][j] = ' ';
    }
  }
}

void displayboard(char board[ROW][COL], int row, int col)
{
  int i = 0;
  for (i = 0; i < row; i++)
  {
    int j = 0;
    for (j = 0; j < col; j++)
    {
      printf(" %c ", board[i][j]);
      if (j < col - 1)
        printf("|");//列印一行的資料
    }
    printf("\n");
    if (i < row - 1)
    {
      for (j = 0; j < col; j++)
      {
        printf("---");
        if (j < col - 1)
          printf("|");
      }
      printf("\n");
    }

  }
}

void player1move(char board[ROW][COL], int row, int col)
{
  int x = 0;
  int y = 0;
  printf("玩家1下棋。\n");
  while (1)
  {
    printf("請玩家1輸入棋盤坐标:>");
    scanf("%d%d", &x, &y);
    //判斷x,y坐标的合法性
    if (x >= 1 && x <= row && y >= 1 && y <= col)
    {
      if (board[x - 1][y - 1] == ' ')
      {
        board[x - 1][y - 1] = '*';
        break;
      }
      else
      {
        printf("該坐标被占用。\n");
      }
    }
    else
    {
      printf("坐标非法,請重新輸入:>\n");
    }
  }
}

void player2move(char board[ROW][COL], int row, int col)
{
  int x = 0;
  int y = 0;
  printf("玩家2下棋。\n");
  while (1)
  {
    printf("請玩家2輸入棋盤坐标:>");
    scanf("%d%d", &x, &y);
    //判斷x,y坐标的合法性
    if (x >= 1 && x <= row && y >= 1 && y <= col)
    {
      if (board[x - 1][y - 1] == ' ')
      {
        board[x - 1][y - 1] = '#';
        break;
      }
      else
      {
        printf("該坐标被占用。\n");
      }
    }
    else
    {
      printf("坐标非法,請重新輸入:>\n");
    }
  }
}

//傳回1表示棋盤滿了,傳回0表示棋盤未滿
int isfull(char board[ROW][COL], int row, int col)
{
  int i = 0;
  int j = 0;
  for (i = 0; i < row; i++)
  {
    for (j = 0; j < col; j++)
    {
      if (board[i][j] == ' ')
      {
        return 0;
      }
    }
  }
  return 1;
}

char iswin(char board[ROW][COL], int row, int col)
{
  int i = 0;
  for (i = 0; i < row; i++)//判斷橫三行
  {
    if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
      return board[i][0];
  }
  for (i = 0; i < col; i++)//判斷豎三列
  {
    if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
      return board[0][i];
  }
  //判斷兩個對角線
  if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
    return board[1][1];
  if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
    return board[1][1];
  //判斷是否平局
  if (1 == isfull(board, ROW, COL))
  {
    return 'Q';
  }
  return 'C';
}      
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//聲明
void initboard(char board[ROW][COL], int row, int col);
void displayboard(char board[ROW][COL], int row, int col);
void player1move(char board[ROW][COL], int row, int col);
void player2move(char board[ROW][COL], int row, int col);
void computermove(char board[ROW][COL], int row, int col);

//四種遊戲狀态
//1玩家勝利--‘*’
//2電腦勝利--‘#’
//3平局--‘Q’
//4繼續遊戲--‘C’
char iswin(char board[ROW][COL], int row, int col);