0

Im trying to build a tic tac toe game, and for some reason seem to be getting hit by a bug when I try and put a value into any values, it seems to give the selected character(say its player 1, and thus an 'X'), into the values selected by the user(where he wants it to be) but also annoyingly into [1][2] and [2][0] of the array. The code is attached.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace System;
using namespace std;

string game_board[2][2];
bool exit_condition = false;
int x_coords;
int y_coords;
int current_player;
string cp_char;

int returnxwin();
int returnowin();

int main()
{
    cout << "Welcome to Tic-Tac-Toe Console!\n" << endl;

    //Start of game
    while(exit_condition != true){
        cout << "This is the game board: " << endl;
        cout << " - | 1 | 2 | 3 | " << endl;
        cout << " 1 | " << game_board[0][0] <<" | " <<game_board[1][0] << " | " << game_board[2][0] << " | " <<endl;
        cout << " 2 | " << game_board[0][1] <<" | " <<game_board[1][1] << " | " << game_board[2][1] << " | "<<endl;
        cout << " 3 | " << game_board[0][2] <<" | " <<game_board[1][2] << " | " << game_board[2][2] << " | \n"<<endl;

        //TESTING PURPOSES BE SURE TO REMOVE!
        current_player = 1;

        //Says which player is first
        cout << "Player " << current_player << " is first!" << endl;

        //Determines which player is first, and if so, which character to give to them.
        if(current_player == 1)
        {
            cp_char = "X";
        } else if(current_player == 2) 
        {
            cp_char = "Y";
        }

        bool valid_input = false;

        while(valid_input != true){
            cout << "Where would you like to place your " << cp_char << " ? (X co-ordinates(top row))\n" ;
            cin >> x_coords;
            cout << "Where would you like to place your " << cp_char << " ? (Y co-ordinates(side row))\n" ;
            cin >> y_coords;

            if((x_coords <= 3) && (y_coords <=3))
            {
                if(game_board[x_coords-1][y_coords-1] == "")
                {
                    game_board[1][2] = "";
                    game_board[2][0] = "";
                    //Problem Here!
                    game_board[x_coords-1][y_coords-1] = cp_char;
                    break;
                    valid_input = true;
                }
                else
                {
                    cout << "Invalid Input! Somebody has already taken the slot!" << endl;
                }
            }
            else
            {
                cout << "Invalid input! Please enter a number from 1 to 3" << endl;
            }
        }
        //End of Valid Input Loop

        //make sure to remove break; 
        // break;
    }

    system("pause");
    return 0;
}

I haven't finished the game yet, but was at a loss as to why this is happening :s

On another side note, Im also looking for a way to randomly decide whether player 1 or 2 goes first.

Edit: I tried doing [3][3], as suggested but it still gives me an error. Ideas?

2
  • 2
    A 2x2 game board seems smaller than the one I used to play on. Commented Feb 17, 2012 at 22:08
  • 1
    Please, where is the question? It supposes the "?" somewhere. Commented Feb 17, 2012 at 22:11

2 Answers 2

2

game_board is defined as an array of 2x2 strings, should it be 3x3?

string game_board[3][3]

I guess you are going over the array so you get undefined results..

Regarding having a way to randomly choose which player will start just use the rand() function:

srand(time(NULL)); // to initialize the random sequence

current_player = rand()%2 + 1; //player will be either 1 or 2
Sign up to request clarification or add additional context in comments.

Comments

2
string game_board[2][2];

This allocates a game board 2 entries wide by 2 entries tall. That's one smaller than a USTTTA / IOCTTT - sanctioned tournament game board. Try this:

std::string game_board[3][3];

3 Comments

std:: being a crucial improvement, I suppose
Yes. I won't write code that relies upon using namespace std;.
@Rob Why is using namespace std reliable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.