C語言實現簡易的三子棋遊戲
本文實例為大傢分享瞭C語言實現三子棋遊戲的具體代碼,供大傢參考,具體內容如下
1.遊戲介紹
三子棋是一種民間傳統遊戲,又叫九宮棋、圈圈叉叉、一條龍等。將正方形對角線連起來,相對兩邊依次擺上三個雙方棋子,隻要將自己的三個棋子走成一條線,對方就算輸瞭。
2.遊戲效果
3.源代碼
(1)頭文件命名為為game.h
主要是整個程序中函數的聲明
#include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 //行 #define COL 3 //列 void BoardInit(char board[ROW][COL], int row, int col); //棋盤展示 void BoardShow(char board[ROW][COL], int row, int col); //人下一個位置用M表示,電腦下一個位置用C表示 void PlayerMove(char board[ROW][COL], int row, int col); void ComputerMove(char board[ROW][COL], int row, int col); //如果人贏返回M 電腦贏返回C 平局返回Q char IsWin(char board[ROW][COL], int row, int col); char IsFull(char board[ROW][COL], int row, int col);
(2)遊戲文件命名為game.c
主要是三子棋遊戲的實現
#include"game.h" #include<stdlib.h> //棋盤初始化 void BoardInit(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 BoardShow(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { printf("-------------------\n"); printf("| | | |\n"); printf("| | | |\n"); for (int j = 0; j < col; j++) { printf(" %c ",board[i][j]); } printf("\n"); } printf("-------------------\n"); } //玩傢下子 void PlayerMove(char board[ROW][COL], int row, int col) { int x, y; while (1) { printf("請輸入你要下的坐標:>"); scanf_s("%d %d", &x, &y); //對玩傢落子坐標判斷 if (x > row || y > col || x < 0 || y < 0) { printf("非法輸入請重新輸入\n"); continue; } else if (board[x][y] != ' ') { printf("此位置已落子,請重新輸入!\n"); continue; } //滿足落子要求返回M else { board[x][y] = 'M'; return 'M'; break; } } } //電腦下子 void ComputerMove(char board[ROW][COL], int row, int col) { srand(time(NULL)); while (1) { int x = rand() % 3; int y = rand() % 3; //滿足落子要求返回C if (board[x][y] != " ") { board[x][y] = 'C'; return 'C'; break; } } } //判斷棋盤是否被下滿 char IsFull(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 'Q'; //滿瞭 } //輸贏判斷 char IsWin(char board[ROW][COL], int row, int col) { //橫行三個連子贏 for (int i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) { if (board[i][0] == 'M') { return 'M'; } else if (board[i][0] == 'C') { return 'C'; } } } //豎行三個個連子贏 for (int i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) { if (board[0][i] == 'M') { return 'M'; } else if (board[0][i] == 'C') { return 'C'; } } } // 斜向"\"這樣三個連子贏 if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) { if (board[1][1] == 'M') { return 'M'; } else if (board[1][1] == 'C') { return 'C'; } } // 斜向"/"這樣三個連子贏 if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) { if (board[1][1] == 'M') { return 'M'; } else if (board[1][1] == 'C') { return 'C'; } } //判斷平局,即棋盤下滿為平局 if ((IsFull(board, ROW, COL)) == 1) { return 'Q'; } return 1; }
(3)測試文件命名為test.c
主要實現程序的測試
#include<stdio.h> #include "game.h" #include<stdlib.h> // 目錄 void meun() { printf("********************************\n"); printf("************1.三子棋************\n"); printf("************2.五子棋************\n"); printf("************0.退出**************\n"); printf("********************************\n"); } //三子棋 void ThressChess() { char board[ROW][COL]; BoardInit(board, ROW, COL); BoardShow(board, ROW, COL); char ret = IsWin(board, ROW, COL); while (1) { //對玩傢下的做判斷 printf("玩傢出手\n"); PlayerMove(board, ROW, COL); BoardShow(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret == 'C') { printf("電腦贏瞭\n"); break; } else if (ret == 'M') { printf("恭喜你贏瞭!\n"); break; } else if (ret == 'Q') { printf("平局\n"); break; } //對電腦下的做判斷 ComputerMove(board, ROW, COL); BoardShow(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret == 'M') { printf("恭喜你贏瞭!\n"); break; } else if (ret == 'C') { printf("電腦贏瞭\n"); break; } else if (ret == '1') { printf("平局\n"); break; } else { printf("繼續\n"); } } } int main() { int input; srand(time(NULL)); do { meun(); printf("請選擇:"); scanf_s("%d", &input); switch (input) { case 1: ThressChess(); break; case 2: printf("正在研發,敬請期待!\n"); break; case 0: printf("按任意鍵退出\n"); break; default: printf("非法輸入,請重新輸入\n"); break; } } while (input); return 0; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。