Untitled

 avatar
unknown
plain_text
9 months ago
3.4 kB
3
Indexable
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// 實作你自己的九宮格
 char map[3][3];
    const char CIRCLE = 'O';
    const char FORK = 'X';
    const char NONE = '-';

// 初始化九宮格
void Initial(){
    for(int i=0 ; i<3 ; i++){
        for(int j=0 ; j<3 ; j++){
            map[i][j]=NONE;
        }
    }
}
// 印出九宮格
void PrintMap(){
     for(int i=0 ; i<3 ; i++){
        for(int j=0 ; j<3 ; j++){
                cout<<map[i][j]<<" ";
        }                    cout<<endl;
    }cout<<"---------"<<endl;
}

bool CheckIsPosValid(int row, int col){
    // Todo : 檢查輸入的行跟列是否正確以及格子內有沒有被填寫
    // Todo (1) : 檢查行跟列的數字有沒有在0-2之間
    if(row < 0 || row > 2 || col < 0 || col > 2) {
        return false;
    }
    // Todo (2) : 檢查行跟列對應的格子沒有被填入圈圈(CIRCLE)或叉叉(FORK)
    if(map[row][col] != NONE) {
        return false;
    }
    return true;
}


// 實作使用者輸入
void UserAction(){
    int place_row = 0;
    int place_col = 0;
    cout<<"Please input two numbers (row, col):";
    cin>>place_row>>place_col;
while(CheckIsPosValid(place_row, place_col) ==false){
        cout<<"Please input two numbers (row, col):";
        cin>>place_row>>place_col;
        }

map[place_row][place_col] =CIRCLE;
}

// 實作AI戰略
void AIAction(){
     for(int i=0 ; i<3 ; i++){
        for(int j=0 ; j<3 ; j++){
            if(map[i][j] == NONE){
                map[i][j] = FORK;
                return;
            }
        }
    }
}
// 檢查遊戲是否結束並印出輸贏結果
bool CheckGameIsOver(){
for(int i=0 ; i<3 ; i++){
    if(map[i][0] == map[i][1] && map[i][1] == map[i][2] && map[i][0] !=NONE){
        if(map[i][0] == CIRCLE)
            cout<<"You win"<<endl;
        else{
            cout<<"You lose"<<endl;

                    }
    return true;}
    }
    for(int j=0 ; j<3 ; j++){
    if(map[0][j] == map[1][j] && map[1][j] == map[2][j] &&map[0][j] !=NONE){
        if(map[0][j] == CIRCLE)
            cout<<"You win"<<endl;
        else{
            cout<<"You lose"<<endl;

                    }
    return true;}
    }
     if(map[0][0] == map[1][1] && map[1][1] == map[2][2] && map[0][0] !=NONE){
        if(map[0][0] == CIRCLE)
            cout<<"You win"<<endl;
        else{
            cout<<"You lose"<<endl;

                    }
    return true;}
    if(map[0][2] == map[1][1] && map[1][1] == map[2][0] && map[2][0] !=NONE){
        if(map[2][0] == CIRCLE)
            cout<<"You win"<<endl;
        else{
            cout<<"You lose"<<endl;

                    }
    return true;}

    return false;
}
int main(){

    // 初始化九宮格
    cout<<"Weclome come to tic-tac-toe game!"<<endl;
    Initial();
    // 開始遊戲
    int turn = 1;
    while(CheckGameIsOver() == false){
        if(turn == 1){
            // 換玩家1開始動作
            cout<<"It's your turn!"<<endl;
            UserAction();
        }else{
            // 換AI開始動作
            cout<<"It's AI turn!"<<endl;
            AIAction();
        }
        // 顯示目前九宮格狀態
        PrintMap();
        turn = -turn;
    }
    cout<<"Game is over!"<<endl;
    return 0;
}
Editor is loading...
Leave a Comment