C語言實現雙人五子棋遊戲
本文實例為大傢分享瞭C語言實現雙人五子棋遊戲的具體代碼,供大傢參考,具體內容如下
實現功能
生成棋盤玩傢1與玩傢2對戰,哪個玩傢率先有連續5子連線,哪個玩傢贏。
如何實現
組成:
二維數組:board[ROW][COL],定義一個ROW*COL的棋盤。
主要邏輯:
顯示棋盤,提示用戶下子,下子後判斷
1.顯示棋盤很簡單,慢慢湊棋盤就好
2. 用戶下子,註意兩個條件:棋子在棋盤裡,下子位置未被占用。
3.判斷是最難的,
方法:從下子位置的8個方向(上,下,左,右,右上,右下,左上,左下)計算相同棋子數目,然後將對角的棋子數相加,等於5說明有5子連線
主要函數中用到三個主要實現函數:
Showboard(board, ROW, COL);//展示棋盤 Playermove(board, ROW, COL, cur);//玩傢下註,cur表示哪個玩傢下子 Judge(board, ROW, COL);//判斷5子連線 Getcount(board[][COL], row, col, dir)//計算方向dir相同棋子數
代碼
頭文件
#ifndef __FIVECHREE_H__ #define __FIVECHEE_H__ #include<stdio.h> #include<windows.h> #pragma warning(disable:4996) #define ROW 10//棋盤行數 #define COL 10//棋盤列數 #define INIT '*'//棋盤初始化 #define PLAYER1 1 #define PLAYER2 2 #define NEXT 3//繼續往下下 #define DRAW 4//棋盤下滿 平局 //8個方向 #define UP 10 #define RIGHT_UP 11 #define RIGHT 12 #define RIGHT_DOWN 13 #define DOWN 14 #define LEFT_DOWN 15 #define LEFT 16 #define LEFT_UP 17 extern void Menu(); extern void Game(); #endif
main函數源文件
#include"fivechree.h" int main(){ int quit = 0; while (!quit){ Menu(); int select = 0; scanf("%d", &select); switch (select){ case 1: Game(); break; case 2: quit = 1; break; default: printf("Enter Error!\n"); break; } } printf("Byebye\n"); system("pause"); return 0; }
函數定義源文件
#include"fivechree.h" static int x = 0; static int y = 0; void Menu(){ printf("+---------------------+\n"); printf("+- 1.Play 2.Exit -+\n"); printf("+---------------------+\n"); printf("Please Enter Your Select#"); } static void Showboard(int board[][COL], int row, int col){//展示棋盤 o玩傢1棋子,x玩傢2棋子 system("cls"); for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ switch (board[i][j]){ case PLAYER1: board[i][j] = 'o'; break; case PLAYER2: board[i][j] = 'x'; break; case 0: board[i][j] = INIT; break; default: break; } } } printf(" "); for (int i =1; i <= row; i++){ printf("%2d ", i); } printf("\n"); for (int i = 1; i <= row; i++){ printf("%-2d", i); for (int j = 1; j <= col; j++){ printf(" %c ", board[i - 1][j - 1]); } printf("\n"); } } static void Playermove(int board[][COL], int row, int col, int who){//玩傢下子,who 為哪個玩傢下子 while (1){ printf("Please Enter PLAYER%d Postion<x,y>#", who); scanf("%d %d", &x, &y); if (x<1 || x>row || y<1 || y>col){ //超過棋盤范圍 printf("Postion is error!\n"); continue; } if (board[x - 1][y - 1] == INIT){//判斷位置是否已被下子 board[x - 1][y - 1] = who; break; } printf("Postion is not empty\n"); } } static int Getcount(int board[][COL], int row, int col, int dir){//判斷8個方向相同棋子的數目 int _x = x;//_x,_y變化,後面與x,y棋子相比較 int _y = y; int count = 0; while (1){ switch (dir){ case UP: _x--; break; case DOWN: _x++; break; case LEFT: _y--; break; case RIGHT: _y++; break; case RIGHT_UP: _x--, _y++; break; case RIGHT_DOWN: _x++, _y++; break; case LEFT_DOWN: _x++, _y--; break; case LEFT_UP: _x--, _y--; break; default: break; } if (_x>=1 || _x<=row || _y>=1 || _y<=col){//棋子不能越界 if (board[x-1][y-1] == board[_x-1][_y-1]){ //printf("yes\n"); count++; } else{ //printf("no\n"); break; } } else{ return count; } } return count; } //如何判斷:從下子位置的8個方向(上,下,左,右,右上,右下,左上,左下) //計算相同棋子數目,然後將對角的棋子數相加,等於5說明有5子連線 static int Judge(int board[][COL], int row, int col){ int count1 = Getcount(board, row, col, UP)\ + Getcount(board, row, col, DOWN); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = Getcount(board, row, col, RIGHT_UP)\ + Getcount(board, row, col, LEFT_DOWN); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = Getcount(board, row, col, RIGHT)\ + Getcount(board, row, col, LEFT); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = Getcount(board, row, col, RIGHT_DOWN)\ + Getcount(board, row, col, LEFT_UP); if (count1 >= 4){ return board[x-1][y-1]; } for (int i = 0; i < row; i++){//判斷棋盤是否下滿 for (int j = 0; j < col; j++){ if (board[i][j] == INIT){ return NEXT; } } } return DRAW; } void Game(){ int board[ROW][COL] = { 0 }; //memset(board, INIT, ROW*COL); int result = 0; int cur = PLAYER1; Showboard(board, ROW, COL);//先展示棋盤 while (1){ //Showboard(board, ROW, COL); Playermove(board, ROW, COL, cur); Showboard(board, ROW, COL);//棋盤將board數組變化,所以要在判斷前將數組變化 result = Judge(board, ROW, COL); if (result != NEXT){ break; } cur = (cur == PLAYER1 ? PLAYER2 : PLAYER1);//三目表達式,註意不是 PLAYER1 ? PLAYER2 : PLAYER1 } Showboard(board, ROW, COL); switch (result){ case 'o': printf("Player1 Win!\n"); break; case 'x': printf("Player2 Win!\n"); break; case DRAW: printf("Tie Game!\n"); break; default: //printf("%c\n", result); printf("BUG\n"); break; } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。