c語言三字棋

2021-08-13 10:38:49 字數 3043 閱讀 5958

1.三字棋存在只能演算法,雖然程式較笨,希望大佬指導。

2.利用for迴圈巢狀if判斷,每一行有三種堵棋的情況。

3.玩家或者電腦每下一步棋都要進行判斷輸贏和列印棋盤

game.h 標頭檔案

#ifndef __game_h__

#define __game_h__

#include#include#include#define row 3

#define col 3

void printmenu();

void printboard(char board[row][col],int row,int col);

void initboard(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 checkwin(char board[row][col], int row, int col);

#endif __game_h__

test.c

#include"game.h"

void game()

; char win = ' ';

initboard(board,row,col);

printboard(board,row,col);

do while (1);

if (win == 'x')

else if (win == '0')

else if (win == 'q')

printboard(board, row, col);

} int main()

} while (choose);

system("pause");

}

game.c
#include"game.h"

void printmenu()

void printboard(char board[row][col], int row, int col)

} printf("---------------\n");

}void initboard(char board[row][col], int row, int col) }}

static int isfull(char board[row][col], int row, int col)

} }return 1;

}void playermove(char board[row][col], int row, int col)

else

}else

}}void computermove(char board[row][col], int row, int col)

else if (board[i][1] == board[i][2] && board[i][1] == 'x')

else if (board[i][0] == board[i][2] && board[i][0] == 'x')

} //行堵棋

if ((board[0][i] == board[1][i] || board[0][i] == board[2][i] || board[1][i] == board[2][i]) && (board[0][i] == 'x' || board[1][i] == 'x' || board[2][i] == 'x'))

else if (board[1][i] == board[2][i] && board[1][i] == 'x')

else if (board[0][i] == board[2][i] && board[0][i] == 'x')

}//列堵棋

if ((board[0][0] == board[1][1] || board[0][0] == board[2][2] || board[1][1] == board[2][2]) && (board[0][0] == 'x' || board[1][1] == 'x' || board[2][2] == 'x'))

else if (board[1][1] == board[2][2] && board[1][1] == 'x')

else if (board[0][0] == board[2][2] && board[0][0] == 'x')

}//主對角線堵棋

if ((board[0][2] == board[1][1] || board[1][1] == board[2][0] || board[0][2] == board[2][0]) && (board[0][2] == 'x' || board[1][1] == 'x' || board[2][0] == 'x'))

else if (board[2][0] == board[1][1] && board[1][1] == 'x')

else if (board[0][2] == board[2][0] && board[0][2] == 'x')

}//非主對角線堵棋

if (count == 0)

} }}

char checkwin(char board[row][col], int row, int col)

}for (i = 0; i < row; i++)

}if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')

if (isfull(board, row, col) == 1)

return ' ';

}

C語言 三字棋

目錄 三字棋小遊戲 第一部分 程式 第二部分.遇到問題及解決方案 重複包含標頭檔案。第三部分 知識擴充套件 三子棋,民間傳統遊戲,又叫九宮棋 圈圈叉叉 一條龍等。將正方形 對角線連起來,相對兩邊依次擺上三個雙方棋子,只要將自己的三個棋子走成 一條線,對方就算輸了。如果兩個人都掌握了技巧,那麼一般來說...

三字棋 C語言實現

三子棋是一種民間傳統遊戲,又叫九宮棋 圈圈叉叉 一條龍等。將正方形對角線連起來,相對兩邊依次擺上三個雙方棋子,只要將自己的三個棋子走成一條線,對方就算輸了。專案概要 使用c語言中二維陣列和函式的基本知識簡單實現乙個三子棋遊戲,這個遊戲要實現的基本功能主要有初始化棋盤 棋盤的列印 玩家下棋 電腦下棋 ...

C語言實現三字棋遊戲

include include include include define rows 3 define cols 3 void menu void init board char arr cols int x,int y 初始化棋盤 void print board char arr cols i...