C ++ Tic Tac Toe游戏 [英] C++ Tic Tac Toe Game

查看:171
本文介绍了C ++ Tic Tac Toe游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++ Tic Tac Toe Game

这是我到目前为止,请帮助我。这是我需要完成的代码。

I tried my best and this is what I have so far, please help me out. This is my code that I need to complete.


  1. 实现displayBoard以显示Tic Tac Toe板。

  2. 提示用户在板上选择一个框,即1到9之间的数字,1是左上角。

  1. Implement displayBoard to display Tic Tac Toe board.
  2. Prompt User for a box on the board to select, i.e. a number between 1 and 9 with 1 being the upper left corner.

使用cin.get (框)获取框编号,并使用isdigit来验证它是一个
编号;
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
如果盒子可用,在其中放置适当的X或O并切换播放器,即X变为O,反之亦然。
如果框不可用,警告用户并获得另一个框,直到他们选择有效的打开框。

use cin.get(box) to get the box number and isdigit to verify it is a number; 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 If the box is available put the appropriate X or O in there and switch players, i.e. X becomes O and vice versa. If the box is NOT available warn the user and get another box until they select a valid open box.

Game Over!;

After all spots have been select Display "Game Over!";

编写一个main函数以使用TicTacToe类并测试所有上述功能。

Write a main function to use the TicTacToe class and test all of the above functionality.

#include <iostream>
using namespace std;


class TicTacToe {
public:
void displayBoard();
void getMove();
void playGame();
private:
char board[9];
char player; // Switch after each move.
};

int main ()
{
TicTacToe ttt;

// you need to do the following in a loop 9 times
ttt.playGame();
}

void TicTacToe::playGame()
{
getMove();
// Your implementation here...
}

void TicTacToe::displayBoard()
{
// Your implementation here...
}

void TicTacToe::getMove()
{
cout << "Enter Box: ";
char c;
cin.get(c);
if (c > '9' || c < '0')
    // Error message here.

int number = c - '0';

cout << "your number is " << number;
// Your implementation here...
}







这是我到目前为止的情况。


This is what I have so far.

#include <iostream> 

using namespace std;

void displayBoard() ;
char cSquare1('1');
char cSquare2('2');
char cSquare3('3');
char cSquare4('4');
char cSquare5('5');
char cSquare6('6');
char cSquare7('7');
char cSquare8('8');
char cSquare9('9');
int iPlayerTurn(1);
bool bGameOver(true);


do {
    // Display Board
    std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << std::endl;

    // Switch After Each Move
    char cPlayerMark;
    if (iPlayerTurn == 1) {
        cPlayerMark = 'X';
    } else {
        cPlayerMark = 'O';
    }

    // Play Game
    std::cout << "Player" << iPlayerTurn << "'s move Enter Box: " << std::endl;
    bool bValidMove;
    // Loop until the Move is Valid
    do {
        char cNextMove;
        std::cin >> cNextMove;
        bValidMove = true;

        // Check for a valid move
        if (cNextMove == '1' && cSquare1 == '1') {
            cSquare1 = cPlayerMark;
        } else if (cNextMove == '2' && cSquare2 == '2') {
            cSquare2 = cPlayerMark;
        } else if (cNextMove == '3' && cSquare3 == '3') {
            cSquare3 = cPlayerMark;
        } else if (cNextMove == '4' && cSquare4 == '4') {
            cSquare4 = cPlayerMark;
        } else if (cNextMove == '5' && cSquare5 == '5') {
            cSquare5 = cPlayerMark;
        } else if (cNextMove == '6' && cSquare6 == '6') {
            cSquare6 = cPlayerMark;
        } else if (cNextMove == '7' && cSquare7 == '7') {
            cSquare7 = cPlayerMark;
        } else if (cNextMove == '8' && cSquare8 == '8') {
            cSquare8 = cPlayerMark;
        } else if (cNextMove == '9' && cSquare9 == '9') {
            cSquare9 = cPlayerMark;
        } else if (cNextMove > '9' || cNextMove < '0')

            std::cout << "error!"." << std::endl;
        return;
                }

                int number = cNextMove - '0';
          cout << "your number is " << number;

    } while (!bValidMove);

    bGameOver       = false;
    bool bWinGame   = true;
    // Check for end of game 
    if (cSquare1 != '1') {
        if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
            bGameOver = true;
        }
        if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
            bGameOver = true;
        }
    }
    if (cSquare5 != '5') {
        if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
            bGameOver = true;
        }
        if (cSquare2 == cSquare5 && cSquare8 == cSquare5) {
            bGameOver = true;
        }
        if (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
            bGameOver = true;
        }
        if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
            bGameOver = true;
        }
    }
    if (cSquare9 != '9') {
        if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
            bGameOver = true;
        }
        if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
            bGameOver = true;
        }
    }
    // Check For Tie Game
    if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
        cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
        cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' &&        

!bGameOver)
    {
        bGameOver = true;
        bWinGame = false;
    }

    if (bGameOver) {
        if (bWinGame) {
            std::cout << "Player" << iPlayerTurn << " wins!" <<   
std::endl;
        }
        // Display Board
        std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 <<  
std::endl;
        std::cout << "-+-+-"<< std::endl;
        std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << 
std::endl;
        std::cout << "-+-+-"<< std::endl;
        std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 <<   
std::endl;

        std::cout << "Game Over!" << std::endl;


推荐答案

我假设这是一个作业或作业问题?

I'm assuming this is an assignment or homework problem?

请注意,它要求您填写一些空方法的实现。所以你需要工作你写在第一个列表的框架中的代码。还要注意,该列表中的TicTacToe类定义了具有char数组的板。

Notice that it's asking you to fill in the implementation of some empty methods. So you need to work the code you've written into the framework of the first listing. Note also that the TicTacToe class in that listing defines the board with a array of char.

因此,例如:

void TicTacToe::displayBoard(){
    std::cout << board[1] << "|" << board[2]<< "|" << board[3]<< std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << board[4] << "|" << board[5]<< "|" << board[6]<< std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << board[7] << "|" << board[8]<< "|" << board[9]<< std::endl;
}

这篇关于C ++ Tic Tac Toe游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆