2012-02-17 6 views
0

私はチックタックのつま先のゲームを構築しようとしていますが、何らかの理由で値を入力しようとするとバグが発生するようですが、 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; 
} 

:S

別のサイドノートでは、イムはまた、ランダムプレイヤー1かどうかを決定する方法を探してまたは2が先に進む。

編集:私は[3] [3]をやってみましたが、それでも私にはエラーが出ます。アイデア?

+2

2x2のゲームボードは、私が使っていたゲームボードよりも小さく見えます。 –

+1

質問はどこですか?それは "?"どこかに。 – Gangnus

答えて

2

は、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 ::'が重要な改善であると私は思います –

+1

はい。私は 'namespace std;を使用しているコードを書くつもりはありません。 –

+0

@Robなぜnamespace stdを使用しているのですか? – Azerty560