在TicTacToe类

时间:2016-06-22 12:19:57

标签: c++ arrays eclipse

有人向我指出here将一个董事会数组传递给tictactoe类的成员函数是一个不好的做法。将它作为一个私有成员变量并在内部操纵它而没有主要知道它更有意义。

我试图在我的代码中实现此更改,现在我在尝试将主板打印到控制台时收到非常奇怪的字符输出:

输出图像

Strange Character Output

我刚刚开始使用课程,所以我想我已经犯了一个基本错误,并以这种方式显示出来。有什么想法吗?

主要

    //implementation of TicTacToe
//Using classes this time

#include <iostream>
#include "TicTacToeClass.h"


int main()
{

    //Assumes no play unless user decides they want to play and initializes game variable to TicTacToe class
    bool play = false;
    TicTacToe game;

    play = game.getUserWantToPlay();

    //allows for multiple games to be played
    while(play == true)
    {

        char playerWinner = 'n';
        char player = 'X';

        //single game iteration
        while(playerWinner == 'n')
        {

            game.drawBoard();
            game.getPlayerMove(player);
            playerWinner = game.checkForWin(player);

            if(playerWinner == 'n')
            {
                player = game.togglePlayer(player);
            }
        }

        game.drawBoard();

        play = game.getUserWantToPlay();

    }

    return(0);
}

班级标题

* TicTacToeClass.h
 *
 *  Created on: Jun 15, 2016
 *      
 */

#ifndef TICTACTOECLASS_H_
#define TICTACTOECLASS_H_


class TicTacToe
{

    public:
        bool getUserWantToPlay();
        void drawBoard();
        void getPlayerMove(char player);
        char togglePlayer(char player);
        char checkForWin(char player);

    private:
        char squareArray[9];

};

#endif /* TICTACTOECLASS_H_ */

班级实施

//TicTacToe class implementation
//Leeroy Jenkins

#include "TicTacToeClass.h"
#include <iostream>

char squareArray[9] = {'1','2', '3', '4', '5', '6', '7', '8', '9'};

bool TicTacToe::getUserWantToPlay()
{

    char response;
    bool invalidResponse = true;
    bool play = false;

    while(invalidResponse == true)
    {

        std::cout << "Would you like to play a new game of TicTacToe? (y/n) " << std::endl;
        std::cin >> response;

        if(response == 'y')
        {
            invalidResponse = false;

            play = true;
        }
        else if(response == 'n')
        {
            std::cout << "No Problem!";
            invalidResponse = false;
        }
        else
        {
            std::cout << "Please input a proper response (y/n)" << std::endl;
        }

    }

    return play;
}
void TicTacToe::drawBoard()
{

    //draws the game board with updated characters for each player

        std::cout << "Player 1 (X) - Player 2 (O)" << std::endl << std::endl << std::endl;

        std::cout << "    |     |" << std::endl;
        std::cout << " " << squareArray[0] << "  |  " << squareArray[1] << "  |  " << squareArray[2] << std::endl;

        std::cout << "____|_____|____" << std::endl;
        std::cout << "    |     |    " << std::endl;

        std::cout << " " << squareArray[3] << "  |  " << squareArray[4] << "  |  " << squareArray[5] << std::endl;

        std::cout << "____|_____|____" << std::endl;
        std::cout << "    |     |    " << std::endl;

        std::cout << " " << squareArray[6] << "  |  " << squareArray[7] << "  |  " << squareArray[8] << std::endl;

}

void TicTacToe::getPlayerMove(char player)
{
    //Gets player move and stores in board array for display through next iteration

    bool playerMoveFound = false;
    char playerTurn = '0';
    char playerMove = '0';

    if(player == 'X')
    {
        playerTurn = '1';
    }
    else
    {
        playerTurn = '2';
    }


    while(playerMoveFound == false)
    {
        std::cout << "Player " << playerTurn << " please make a move" << std::endl;
        std::cin >> playerMove;

        for(int x = 0; x < 9; x++)
        {
            //If finds the array number makes the change to the iteration...prevents x or o movement
            if(playerMove == squareArray[x] && playerMove != 'X' && playerMove != 'O' && playerMove != 'x' && playerMove != 'o')
            {
                squareArray[x] = player;

                playerMoveFound = true;
            }

        }
        if(playerMoveFound == false)
        {
            std::cout << "Invalid player move..." << std::endl;
        }
    }
}

char TicTacToe::checkForWin(char player)
{
    char playerWin = 'n';
    int testForTie = 0;

    //Tests winning combinations
    if(squareArray[0] == squareArray[1] && squareArray[1] == squareArray[2])
        {
            playerWin = player;
        }
        else if(squareArray[0] == squareArray[3] && squareArray[3] == squareArray[6])
        {
            playerWin = player;
        }
        else if(squareArray[0] == squareArray[4] && squareArray[4] == squareArray[8])
        {
            playerWin = player;
        }
        else if(squareArray[1] == squareArray[4] && squareArray[4] == squareArray[7])
        {
            playerWin = player;
        }
        else if(squareArray[2] == squareArray[4] && squareArray[4] == squareArray[6])
        {
            playerWin = player;
        }
        else if(squareArray[2] == squareArray[5] && squareArray[5] == squareArray[8])
        {
            playerWin = player;
        }
        else if(squareArray[3] == squareArray[4] && squareArray[4] == squareArray[5])
        {
            playerWin = player;
        }
        else if(squareArray[6] == squareArray[7] && squareArray[7] == squareArray[8])
        {
            playerWin = player;
        }
        else
        {
            //Tests for a tie game
            for(int x = 0; x < 9; x++)
            {
                if(squareArray[x] == 'x' || squareArray[x] == 'o' || squareArray[x] == 'X' || squareArray[x] == 'O')
                {
                    testForTie++;
                }
            }

            if(testForTie == 9)
            {

                playerWin = 't';
            }
        }

    if(playerWin == player)
    {
        if(player == 'X')
        {
            std::cout << std::endl << "Congratulations player 1!  You Win!" << std::endl;
        }
        else
        {
            std::cout << std::endl << "Congratulations player 2!  You Win!" << std::endl;
        }
    }
    else if(playerWin == 't')
    {
        std::cout << "Tie!  You should play again to settle the duel!" << std::endl;
    }

    return(playerWin);

}

char TicTacToe::togglePlayer(char player)
{

    player = player == 'X' ? 'O':'X';

    return(player);

}

2 个答案:

答案 0 :(得分:4)

您的TicTacToe课程没有初始化其squareArray班级成员,即您的游戏主席的构造函数。

char squareArray[9] = {'1','2', '3', '4', '5', '6', '7', '8', '9'};

这在全局范围内声明了一个名为squareArray的变量。它没有初始化同名的班级成员,这与班级成员没有任何关系。

因此,类成员的初始内容由未初始化的内存和随机垃圾组成。您正在查看未定义行为的结果,将未初始化的数组的内容显示为std::cout

你需要有一个类构造函数来相应地初始化类成员。

答案 1 :(得分:3)

您从未在squareArray中初始化TicTacToe,这就是

std::cout << " " << squareArray[0] << "  |  " << squareArray[1] << "  |  " << squareArray[2] << std::endl;

使用。当你做了

char squareArray[9] = {'1','2', '3', '4', '5', '6', '7', '8', '9'};

在实现文件中,创建一个名为squareArray的全局char数组。您没有初始化类成员变量squareArray。您需要编写初始化成员变量的默认构造函数。你可以用

TicTacToe() : squareArray{'1','2', '3', '4', '5', '6', '7', '8', '9'} {}