C語言實現三子棋源代碼

本文實例為大傢分享瞭C語言實現三子棋的具體代碼,供大傢參考,具體內容如下

效果如下:

代碼實現:

頭文件部分

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3

//函數聲明
void Initboard(char board[ROW][COL], int row, int col);
void Displayboard(char board[ROW][COL], int row, int col);
Player(char board[ROW][COL], int row, int col);
Computer(char board[ROW][COL], int row, int col);
char Judge(char board[ROW][COL], int row, int col);

函數部分

#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] = ' ';
}
//棋盤
// x | x | x 
//---|---|---
// x | x | x 
//---|---|---
// x | x | x 
void Displayboard(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++)
 {
 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");
 }
}
 Player(char board[ROW][COL], int row, int col)
{
 while (1)
 {
 int x = 0;
 int y = 0;
 printf("請輸入你的棋子坐標(如第一行第一列為1 1):\n");
 scanf("%d%d", &x, &y);
 if (x <= row && x >= 1 && y <= col && y >= 1)
 {
 if (board[x - 1][y - 1] == ' ')
 {
 board[x - 1][y - 1] = '*';
 break;
 }
 else
 {
 printf("該處已有棋子,請重新輸入\n");
 continue;
 }
 }
 else
 printf("坐標范圍錯誤,請重新輸入");
 }
}
 Computer(char board[ROW][COL], int row, int col)
 {
 int x = 0;
 int y = 0;
 while (1)
 {
 x = rand() % ROW;
 y = rand() % COL;
 if (board[x][y] == ' ')
 {
 board[x][y] = '#';
 break;
 }
 }
 }
 int IsFull(char board[ROW][COL], int row, int col)//返回1表示棋盤已滿,0表示棋盤沒滿
 {
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 if (board[i][j] == ' ')
  return 0;
 } 
 return 1;
 }

 char Judge(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][0] != ' ')
 return board[i][0];
 }
 //列有三個相連的情況
 for (i = 0; i < col; i++)
 {
 if (board[0][i] == board[1][i] && board[1][i]==
 board[2][i] && board[0][i] != ' ')
 return board[0][i];
 }
 //對角線三個相連的情況
 if (board[0][0] == board[1][1]&& board[1][1]==board[2][2]&&board[0][0]!=' '
 || board[0][2] == board[1][1]&&board[1][1] == board[2][0]&&board[0][2] != ' ')
 return board[1][1];
 //平局的情況(棋盤滿瞭)
 if (IsFull( board , ROW,COL) ==1)
 {
 return 'E';
 }
 return 'C';
 }

主函數部分

#include "game.h"

void menu()
{
 printf("***************************\n");
 printf("***1.開始遊戲 0.退出遊戲***\n");
 printf("***************************\n");
 printf("請輸入數字1或0:");
}
void game()
{
 char r;
 char board[ROW][COL] = { 0 };
 //初始化棋盤(清空棋盤)
 Initboard(board, ROW, COL);
 //打印棋盤
 Displayboard(board, ROW, COL);
 //人機對弈
 while (1)
 {
 Player(board, ROW, COL);
 Displayboard(board, ROW, COL);
 r = Judge(board, ROW, COL);
 if (r != 'C')
 break;
 Computer(board, ROW, COL);
 Displayboard(board, ROW, COL);
 r = Judge(board, ROW, COL);
 if (r != 'C')
 break;
 }
 //判斷輸贏
 if (r == '*')
 {
 printf("玩傢贏\n");
 }
 else if (r == '#')
 {
 printf("電腦贏\n");
 }
 else if (r == 'E')
 {
 printf("平局\n");
 }
}
int main()
{
 srand((unsigned int)time(NULL));
 int input = 0;
 do
 {
 menu();
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 printf("開始遊戲\n");
 game();
 break;
 case 0:
 printf("退出遊戲\n");
 break;
 default:
 printf("請輸入正確的選項\n");
 break;
 }
 } while (input);
 printf("遊戲結束!");
 return 0;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: