MODE —— 两个人在计算机上玩圈叉游戏|井字游戏(知识点:二维数组)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                MODE —— 两个人在计算机上玩圈叉游戏|井字游戏(知识点:二维数组)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                問題描述:
????? ? 讓兩個人在計算機上玩井字游戲(也稱圈叉游戲)。
????? ? 井字游戲就是一個3X3的方格,兩個人輪流在方格中輸入標記X 或者 O 。誰先使自己的3個標記鏈接成水平,垂直或對角線。誰就是贏家。?
運行結果:
代碼部分(代碼說明):
#include <stdio.h> int main() { int player = 0; //Current player number -1 or 2?int winner = 0; //The winning player numberunsigned int i = 0;int choice = 0; //Chosen squareunsigned int row = 0; //Row index for a squareunsigned int column = 0; //Column index for a squareunsigned int line = 0;char board[3][3] = { //The board{'1','2','3'}, //Initial values are characters '1' to '9'{'4','5','6'}, //used to select a vacant square{'7','8','9'} //for a player`s turn };//The main game loop, The game continues for up to 9 turns//as loong as there is no winnerfor(i = 0;i < 9 && winner ==0;++i){//Display the boardprintf("\n");printf(" %c | %c | %c \n",board[0][0],board[0][1],board[0][2]);printf("---+---+---\n");printf(" %c | %c | %c \n",board[1][0],board[1][1],board[1][2]);printf("---+---+---\n");printf(" %c | %c | %c \n",board[2][0],board[2][1],board[2][2]);//有一種讓兩個玩家輪流輸入標記的辦法,將兩個玩家識別為1和2,編號為1的玩家先玩。然后根據輪流的次數決定輸入標記的玩家的號碼。//輪到奇數號碼時候 就由玩家1輸入標記。輪到偶數號時,就由玩家2輸入標記。player = i%2 + 1; //Select player 1 or 2//Get valid player square selection do{printf("Player %d,please enter a valid square number""for where you want to place your %c: ",player,(player == 1) ? 'X' : 'O'); //輪到一個玩家輸入標記時,需要一種方法標記選擇出來的方格。可以用1~9的數字標記這9個方格。玩家只需要輸入要選擇的方格數字。scanf("%d",&choice); row = --choice/3; //Get row index of squarecolumn = choice % 3; //Get column index of square}while(choice < 0 || choice > 8 || board[row][column] > '9'); //有三種可能導致選擇無效:*輸入的方格數小于0; *輸入的方格數大于8; *選擇已包含X或者O的方格(因為X O 的字符碼都大于9)//Insert player symbloboard[row][column] = (player == 1) ? 'X' : 'O';//Check for a winning line - diagonals first if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2]== board[2][0]))winner = player;else{//Check row and columns for a winning line for(line = 0;line <= 2;++line ){if((board[line][0] == board[line][1] && board[line][0] == board[line][2]) || (board[0][line] == board[1][line] && board[0][line] == board[2][line]))winner = player;}}/*Code to check for a winner*/}/*Code to output the game*///Game is over so display the final boardprintf("\n");printf(" %c | %c | %c \n",board[0][0],board[0][1],board[0][2]);printf("---+---+---\n");printf(" %c | %c | %c \n",board[1][0],board[1][1],board[1][2]);printf("---+---+---\n");printf(" %c | %c | %c \n",board[2][0],board[2][1],board[2][2]);//Display result message if(winner)printf("\nCongratulation,player %d,YOU ARE THE WONNER!\n",winner);elseprintf("\nHow boring , it is a draw\n");return 0;}總結
以上是生活随笔為你收集整理的MODE —— 两个人在计算机上玩圈叉游戏|井字游戏(知识点:二维数组)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: MODE —— 计算10个分数的平均值(
- 下一篇: 读文件 —— WEB前端读取本地文件内容
