2012-02-17 46 views
0

我试图构建一个tic tac脚趾游戏,并且出于某种原因,当我尝试将值放入任何值时,似乎被某个错误所击中,它似乎给出了选定的字符(比如它的播放器1,因此'X')转换为用户选择的值(他希望的地方),但也令人讨厌地进入阵列的[1] [2]和[2] [0]。该代码已附加。C++数组问题

#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; 
} 

我还没有完成比赛呢,但在亏损,为什么发生这种情况:■

在另一个方面说明,林也在寻找一种方式来随机决定是否播放器1或2先走。

编辑:我试着做[3] [3],建议,但它仍然给我一个错误。想法?

+2

2x2游戏板看起来比我以前玩的小。 – 2012-02-17 22:08:44

+1

请问,问题在哪里?它假设“?”某处。 – Gangnus 2012-02-17 22:11:47

回答

2

game_board被定义为2x2字符串的数组,它应该是3x3吗?

string game_board[3][3] 

我猜你会在阵列上,所以你得到不确定的结果..

关于有一种方法来随机选择哪位球员将开始只使用rand()功能:

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

current_player = rand()%2 + 1; //player will be either 1 or 2 
2
string game_board[2][2]; 

这分配一个游戏板2条目宽2条目高。这比USTTTA/IOCTTT认可的锦标赛游戏板还要小。试试这个:

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

'std ::'是一个重要的改进,我想 – 2012-02-17 22:14:28

+1

是的。我不会编写依赖'using namespace std;'的代码。 – 2012-02-17 22:20:44

+0

@Rob为什么使用namespace std可靠? – Azerty560 2012-02-18 00:17:37