很無聊的作業, 要求是在 console 在執行

而且要求方式感覺很不合我口味,

寫到後來實在沒什麼興趣下去..

enum CHESS {EMPTY, CROSS, CIRCLE};

struct GameStruct
{
    CHESS gameBoard[3][3]; // the 3x3 game board
    CHESS whoseTurn;       // who will be the next to move, initially CROSS
};

void initializeGame(GameStruct& currentGame);
void placeChess(GameStruct& currentGame, int x, int y);

原始碼(main.cpp)

#include <stdio.h>
#include <stdlib.h>
#include "Tic-Tac-Toe.h"
int main()
{
        StartGame();
        return 0;
}

原始碼(Tic-Tac-Toe.h)

#ifndef _TIC_TAC_TOE_
#define _TIC_TAC_TOE_

enum CHESS {EMPTY, CROSS, CIRCLE};

struct GameStruct
{
    CHESS gameBoard[3][3]; // the 3x3 game board
    CHESS whoseTurn;       // who will be the next to move, initially CROSS
};

void StartGame();
void initializeGame(GameStruct& currentGame);
CHESS GetTurn(GameStruct currentGame);
void AskPositon(GameStruct currentGame, int *x, int *y);
void PlaceChess(GameStruct& currentGame, int x, int y);
void ShowBoard(GameStruct currentGame);
const CHESS  Winner(GameStruct currentGame);

#endif

原始碼(Tic-Tac-Toe.cpp)

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "Tic-Tac-Toe.h"

// ==============================================
// 開始遊戲
void StartGame()
{
        int i, x=0, y=0;
        char again;
        const int MAX_TIMES = 9;
        CHESS WINNER;
        GameStruct currentGame;
        do{
                // 1. 初始化遊戲
                initializeGame(currentGame);
                // 2. 進行遊戲
                for(i=0; i<MAX_TIMES; i++) {               
                        // 2.a 顯示棋子, 清畫面
                        system("cls");
                        ShowBoard(currentGame);
                        // 2.b 放棋子
                        PlaceChess(currentGame, x, y);
                        // 2.c 判斷勝負
                        WINNER = Winner(currentGame);
                        // 2.d 顯示勝負
                        if(WINNER==CROSS) {
                                printf("winner is player 1 - CROSS!\n");
                                break;
                        }
                        else if(WINNER==CROSS){
                                printf("winner is player 2 - CIRCLE!\n");
                                break;
                        }
                        else { // current DRAW
                                NULL;
                        }
                }
                if(WINNER==EMPTY) printf("Draw Game!\n");
                ShowBoard(currentGame);
                printf("again(press Y/N)");
                again = getch();
        }while(again=='y' || again == 'Y');
}
// ==============================================
// !要求函數, 初始化棋盤
void initializeGame(GameStruct& currentGame)
{
        int i, j;
        for(i=0; i<3; i++)
                for(j=0; j<3;j++) currentGame.gameBoard[i][j] = EMPTY;
}
// ==============================================
// 取得該誰下手及位置
CHESS GetTurn(GameStruct currentGame)
{
        // 設定目前下旗對象, player1 : CROSS, play2: CIRCLE
        if(currentGame.whoseTurn==EMPTY) printf("player1(CROSS)'s turn:"), currentGame.whoseTurn=CROSS;
        else if(currentGame.whoseTurn==CROSS) printf("player2(CIRCLE)'s turn:"),  currentGame.whoseTurn=CIRCLE;
        else printf("player1(CROSS)'s turn:"), currentGame.whoseTurn=CROSS;
        return currentGame.whoseTurn;
}
// ==============================================
// 詢問欲下棋位置
void AskPositon(GameStruct currentGame, int *x, int *y)
{
        int success;
    do{
                success=1;
                printf(" Where do you want to place your chess?\n");
                printf("Row(1-3):");
                scanf("%d", x);
                printf("Col(1-3):");
                scanf("%d",y);
                if(*x<0 || *y<0 || *x>=3 ||*y >=3) success = 0, printf("input error!\n"); // 輸入範圍錯誤
                else if(currentGame.gameBoard[*x][*y]!=EMPTY) success=0, printf("that position had been placed!\n"); // 位置已被放
                else NULL; /*currentGame.gameBoard[*x][*y]=currentGame.whoseTurn; // 輸入正確, 放棋, 但在此更新無效*/
        }while(success==0);
}
// ==============================================
// !要求函式, 原設計不好
// 呼叫 GetTurn 更新由誰下旗
// 呼叫 AskPosition 問位置
// 最後更新位置
void PlaceChess(GameStruct& currentGame, int x, int y)
{
        currentGame.whoseTurn = GetTurn(currentGame);
        AskPositon(currentGame, &x, &y);
        currentGame.gameBoard[x][y] = currentGame.whoseTurn;
}

// ==============================================
// 顯示目前棋盤狀態
void ShowBoard(GameStruct currentGame)
{
        int i, j;
        for(i=0; i<3; i++){
                for(j=0; j<3;j++){
                        if(currentGame.gameBoard[i][j] == EMPTY) printf("   ");
                        else if(currentGame.gameBoard[i][j] == CROSS) printf(" X ");
                        else printf(" O ");

                        if(j==0 || j==1) printf("|");
                        else printf("\n");
                }
                printf("-----------\n");
        }
}


// ==============================================
// 判斷勝者, 直接暴力判斷
//

const CHESS Winner(GameStruct currentGame)
{
        int i=0, j=0;
        const CHESS WIN_1 = CROSS;
        const CHESS WIN_2 = CIRCLE;
        const CHESS DRAW  = EMPTY;

        CHESS a[3][3];
        for(i=0; i<3; i++) for(j=0; j<3; j++) a[i][j] = currentGame.gameBoard[i][j];
        // =================================
        // CROSS win, WIN_1
        if(a[0][0]==WIN_1 && a[1][1]==WIN_1 && a[2][2]==WIN_1) return WIN_1;
        else if(a[0][2]==WIN_1 && a[1][1]==WIN_1 && a[2][0]==WIN_1) return WIN_1;
        else if(a[0][0]==WIN_1 && a[0][1]==WIN_1 && a[0][2]==WIN_1) return WIN_1;
        else if(a[1][0]==WIN_1 && a[1][1]==WIN_1 && a[1][2]==WIN_1) return WIN_1;
        else if(a[2][0]==WIN_1 && a[2][1]==WIN_1 && a[2][2]==WIN_1) return WIN_1;
        else if(a[0][0]==WIN_1 && a[1][0]==WIN_1 && a[2][0]==WIN_1) return WIN_1;
        else if(a[0][1]==WIN_1 && a[1][1]==WIN_1 && a[2][1]==WIN_1) return WIN_1;
        else if(a[0][2]==WIN_1 && a[1][2]==WIN_1 && a[2][2]==WIN_1) return WIN_1;
        // =================================
        // CIRCLE win, WIN_2
        else if(a[0][0]==WIN_2 && a[1][1]==WIN_2 && a[2][2]==WIN_2) return WIN_2;
        else if(a[0][2]==WIN_2 && a[1][1]==WIN_2 && a[2][0]==WIN_2) return WIN_2;
        else if(a[0][0]==WIN_2 && a[0][1]==WIN_2 && a[0][2]==WIN_2) return WIN_2;
        else if(a[1][0]==WIN_2 && a[1][1]==WIN_2 && a[1][2]==WIN_2) return WIN_2;
        else if(a[2][0]==WIN_2 && a[2][1]==WIN_2 && a[2][2]==WIN_2) return WIN_2;
        else if(a[0][0]==WIN_2 && a[1][0]==WIN_2 && a[2][0]==WIN_2) return WIN_2;
        else if(a[0][1]==WIN_2 && a[1][1]==WIN_2 && a[2][1]==WIN_2) return WIN_2;
        else if(a[0][2]==WIN_2 && a[1][2]==WIN_2 && a[2][2]==WIN_2) return WIN_2;
        // =================================
        // DRAW GAME
        else return DRAW;
}

 

arrow
arrow
    全站熱搜

    Edison 發表在 痞客邦 留言(1) 人氣()