三子棋游戲一、分析 1.創建一個進入游戲讓玩家選擇的框架2.創建一個三子棋的棋盤,棋盤內部存放玩家和電腦下的棋子,所以總的來說棋盤是由一個二維數組和棋盤框架構成的3.對棋盤進行操作4.判斷棋局並做出調整 二、代碼 game.h #define _CRT_SECURE_NO_WARNINGS 1 #i ...
三子棋游戲
一、分析
1.創建一個進入游戲讓玩家選擇的框架
2.創建一個三子棋的棋盤,棋盤內部存放玩家和電腦下的棋子,所以總的來說棋盤是由一個二維數組和棋盤框架構成的
3.對棋盤進行操作
4.判斷棋局並做出調整
二、代碼
game.h
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #define ROW 3 #define COL 3 //初始化數組 void Init(char board[ROW][COL], int row, int col); //列印棋盤 void Display(char board[ROW][COL], int row, int col); //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col); //電腦下棋 void ComputerMove(char board[ROW][COL], int row, int col); //判斷輸贏 char Is_Win(char board[ROW][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void Init(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } } //期待列印的棋盤 // | | //---|---|--- // | | //---|---|--- // | | void Display(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { //列印i行 for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } } //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col) { int x, y; while (1) { printf("Player Move:>"); scanf("%d%d", &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("Location Error\n"); } } else { printf("Location Error\n"); } } } //電腦下棋 void ComputerMove(char board[ROW][COL], int row, int col) { int x, y; //隨機生成一個x,y的位置 printf("Computer Move\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } int Is_Full(char board[ROW][COL], int row, int col) { int flag = 1; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') { flag = 0; return flag; } } } return flag; } //判斷輸贏 //如果返回‘*’代表玩家贏 //‘#’代表電腦贏 //‘D’代表平局 //‘C’代表可以繼續 char Is_Win(char board[ROW][COL], int row, int col) { //玩家贏,電腦贏 //行三個子,列三個子,斜三個子 //行 for (int i = 0; i < row; i++) { for (int j = 0; j <= col - 3; j++) { if ((board[i][j] == board[i][j + 1] )&&( board[i][j + 1] == board[i][j + 2]) &&( board[i][j] != ' ')) { return board[i][j]; } } } //列 for (int j = 0; j < col; j++) { for (int i = 0; i <= row - 3; i++) { if ((board[i][j] == board[i + 1][j] )&&( board[i+1][j] == board[i + 2][j]) &&( board[i][j] != ' ')) { return board[i][j]; } } } //正斜 for (int i = 0; i <= row - 3; i++) { for (int j = 0; j <= col - 3; j++) { if ((board[i][j] == board[i + 1][j+1]) && (board[i + 1][j+1] == board[i + 2][j+2]) && (board[i][j] != ' ')) { return board[i][j]; } } } //反斜 for (int i = row - 1; i >= 2; i--) { for (int j = col - 1; j >= 2; j--) { if ((board[i][j-2] == board[i - 1][j - 1]) && (board[i - 1][j - 1] == board[i - 2][j ]) && (board[i-1][j-1] != ' ')) { return board[i-1][j-1]; } } } //平局 if (Is_Full(board,row,col) == 1) { return 'D'; } else { return 'C'; } //繼續 }
test.c
#include "game.h" void menu() { printf(" \n"); printf(" 1.Play 0.Exit \n"); printf(" \n"); } void game() { char board[ROW][COL]; //初始化數組 Init(board, ROW, COL); //列印棋盤 Display(board, ROW, COL); //開始下棋,是一個迴圈事件 char ret; while (1) { //玩家下棋 PlayerMove(board,ROW,COL); Display(board, ROW, COL); //判斷棋局形勢 ret=Is_Win(board, ROW, COL); //printf("%c\n", ret);//用於測試 if (ret != 'C') { break; } //電腦下子 ComputerMove(board, ROW, COL); Display(board, ROW, COL); ret=Is_Win(board, ROW, COL); //printf("%c\n", ret);//用於測試 if (ret != 'C') { break; } } if (ret == '*') printf("Player Win\n"); else if (ret == '#') printf("Computer Win\n"); else if (ret == 'D') printf("Draw\n"); } int main() { //初始化隨機數 srand((unsigned int)time(NULL)); //創建一個初始界面 int input; do { menu(); printf("Input Num:>"); scanf("%d", &input); switch (input) { case 1: //進入游戲 game(); break; case 0: printf("Exit\n"); break; default: printf("Error Input\n"); break; } } while (input); return 0; }
三、運行結果