2015-11-01 7 views
-1

Я действительно потерян, мне не удается построить эту игру. Мне нужны твои ребята. Уже несколько дней борется за это. У меня есть три класса: «Драйвер», «Совет» и «Игрок». У меня есть водитель, и я думаю, что класс правления под контролем. Я в основном борюсь с классом игроков. Цель этого состоит в том, чтобы компьютерный игрок мог случайным образом вводить в массив, а затем игрок мог вносить свой выбор в том месте, где они хотят играть.Tic Tac Toe: Java

public class Driver 
{ 
    public static void main(String[] args) 
    { 
     //new tic-tac-toe board 
     Board board = new Board(); 

     //two new players (conputer and human) 
     Player computer = new Player(board, "X"); //Give computer player access to board and assign as X. 
     Player human = new Player(board, "O");  //Give human player access to board and assign as O. 
     board.print(); 
     computer.computerMove(); 

     //while the game is not over 
     while(!board.gameOver()) 
     { 
      //let computer move first 
      computer.computerMove(); 

      //print tic-tac-toe board 
      board.print(); 

      //if the game is not over yet 
      if (!board.gameOver()) 
      { 
       //let the human make a move 
       human.humanMove(); 

       //if the game is over 
       if (board.gameOver()) 
       { 
        //print the board 
        board.print(); 
       } 
      } 
     } 

     //print out the winner (if there is one) of the game 
     board.printWinner(); 
    } 
} 

Board класс

public class Board 
{ 
    private String player = "X"; 
    private String cpu = "O"; 
    int row = 3; 
    int column = 3; 
    private String[][] theBoard = new String[row][column] ; 

    public Board() 
    { 
     theBoard = theBoard; 
    } 

    public boolean gameOver() 
    { 
     if (theBoard[0][0] == player && theBoard[0][1] == player && theBoard[0][2] == player || // 1st row 
      theBoard[1][0] == player && theBoard[1][1] == player && theBoard[1][2] == player || // 2nd row 
      theBoard[2][0] == player && theBoard[2][1] == player && theBoard[2][2] == player || // 3rd row 
      theBoard[0][0] == player && theBoard[1][0] == player && theBoard[2][0] == player || // 1st col. 
      theBoard[0][1] == player && theBoard[1][1] == player && theBoard[2][1] == player || // 2nd col. 
      theBoard[0][2] == player && theBoard[1][2] == player && theBoard[2][2] == player || // 3rd col. 
      theBoard[0][0] == player && theBoard[1][1] == player && theBoard[2][2] == player || // Diagonal   \ 
      theBoard[2][0] == player && theBoard[1][1] == player && theBoard[0][2] == player) // Diagonal /
      { 
       return false; 
      } 
     else if (theBoard[0][0] == cpu && theBoard[0][1] == cpu && theBoard[0][2] == cpu || // 1st row 
      theBoard[1][0] == cpu && theBoard[1][1] == cpu && theBoard[1][2] == cpu || // 2nd row 
      theBoard[2][0] == cpu && theBoard[2][1] == cpu && theBoard[2][2] == cpu || // 3rd row 
      theBoard[0][0] == cpu && theBoard[1][0] == cpu && theBoard[2][0] == cpu || // 1st col. 
      theBoard[0][1] == cpu && theBoard[1][1] == cpu && theBoard[2][1] == cpu || // 2nd col. 
      theBoard[0][2] == cpu && theBoard[1][2] == cpu && theBoard[2][2] == cpu || // 3rd col. 
      theBoard[0][0] == cpu && theBoard[1][1] == cpu && theBoard[2][2] == cpu || // Diagonal   \ 
      theBoard[2][0] == cpu && theBoard[1][1] == cpu && theBoard[0][2] == cpu) // Diagonal /

      { 
       return false; 
      } 
     else{ 

      return true; 
     } 
    } 

    public void print() 
    { 

     System.out.println(theBoard[0][0] + " | " + theBoard[0][1]+ " | " + theBoard[0][2] + "\n----------"); 

     System.out.println(theBoard[1][0] + " | " + theBoard[1][1]+ " | " + theBoard[1][2] + "\n----------"); 

     System.out.println(theBoard[2][0] + " | " + theBoard[2][1]+ " | " + theBoard[2][2] + "\n"); 

    } 

    public void printWinner() 
    { 
     if (theBoard[0][0] == player && theBoard[0][1] == player && theBoard[0][2] == player || // 1st row 
      theBoard[1][0] == player && theBoard[1][1] == player && theBoard[1][2] == player || // 2nd row 
      theBoard[2][0] == player && theBoard[2][1] == player && theBoard[2][2] == player || // 3rd row 
      theBoard[0][0] == player && theBoard[1][0] == player && theBoard[2][0] == player || // 1st col. 
      theBoard[0][1] == player && theBoard[1][1] == player && theBoard[2][1] == player || // 2nd col. 
      theBoard[0][2] == player && theBoard[1][2] == player && theBoard[2][2] == player || // 3rd col. 
      theBoard[0][0] == player && theBoard[1][1] == player && theBoard[2][2] == player || // Diagonal   \ 
      theBoard[2][0] == player && theBoard[1][1] == player && theBoard[0][2] == player) // Diagonal /
      { 
       System.out.println("X - won!"); 
      } 
     else if (theBoard[0][0] == cpu && theBoard[0][1] == cpu && theBoard[0][2] == cpu || // 1st row 
      theBoard[1][0] == cpu && theBoard[1][1] == cpu && theBoard[1][2] == cpu || // 2nd row 
      theBoard[2][0] == cpu && theBoard[2][1] == cpu && theBoard[2][2] == cpu || // 3rd row 
      theBoard[0][0] == cpu && theBoard[1][0] == cpu && theBoard[2][0] == cpu || // 1st col. 
      theBoard[0][1] == cpu && theBoard[1][1] == cpu && theBoard[2][1] == cpu || // 2nd col. 
      theBoard[0][2] == cpu && theBoard[1][2] == cpu && theBoard[2][2] == cpu || // 3rd col. 
      theBoard[0][0] == cpu && theBoard[1][1] == cpu && theBoard[2][2] == cpu || // Diagonal   \ 
      theBoard[2][0] == cpu && theBoard[1][1] == cpu && theBoard[0][2] == cpu) // Diagonal /

      { 
       System.out.println("O - won!"); 
      } 


    } 
} 

и класс игрока, это один я борюсь с самым.

import java.util.Scanner; 
import java.util.Random; 
import java.util.Arrays; 
public class Player 

{ 
    String player = "X"; 
    String cpu = "O"; 
    private Board ticTac; 
    public static Scanner scan = new Scanner(System.in); 
    public Player(Board board, String inBoard) 
    { 
     ticTac = board; 
    } 
public void randomPlace() 
    { 
     for(int i = 0; i < 3; i ++) 
     { 
      for(int j = 0; j < 3; j++) 
      { 

      } 
     } 
    } 
    public void computerMove() 
    { 


    }   

    public void humanMove() 
    { 

    } 
} 

печатает

null | null | null 
---------- 
null | null | null 
---------- 
null | null | null 
+0

Вы задали подобный вопрос: http://stackoverflow.com/questions/33446884/random-position-in-string-array-java – Bon

ответ

1

Я думаю, может быть, это может вам помочь.

import java.util.Scanner; 
import java.util.Random; 
import java.util.Arrays; 
public class Player 

{ 
    String player = "X"; 
    String cpu = "O"; 

    int row = 3; 
    int column = 3; 

    private Board ticTac; 


    public static Scanner scan = new Scanner(System.in); 
    public Player(Board board, String inBoard) 
    { 
     //here you have the board in player 
     tictac = board; 
    } 

    public void computerMove() 
    { //here you can code something like this 
     tictac.put(tictac.getRandomFreePlace(),cpu); 
    }   

    public void humanMove(Position position) 
    { 
     tictac.put(position, human); 
    } 
} 

вы должны закодировать в Совете оферту (Позиция, String) и getRandomFreePlace()

Затем отсканируйте для движения игроков и печати платы.

------------- UPDATE --------------

Ах, вы хотите инициализировать плату? Вы можете сделать это с двойной для

for(i=0;i<row;i++){ 
     for(j=0;j<column;j++){ 
     //here you can set the value you want 
     theBoard[i][j]=0; 
     } 
    } 
+0

о том, что Совет (капитал B) нельзя преобразовать в java.lang.String [] []. Спасибо за попытку, хотя я ДОЛЖЕН ценить любые идеи. – pewpew

+0

спасибо, но как вы думаете, я мог бы это сделать без редактирования драйвера? – pewpew

+0

OH nvm Я вижу, что вы сделали;) – pewpew

0

Не уверен, что, если я понял ваш вопрос вы можете повторно слово это или дать мне пример?

Из чего я понял, что вы хотели бы, чтобы ваш метод правления, используемый в вашем классе игрока, был правильным?

Если вы создали класс платы и предоставили ему все необходимые характеристики, то внутри класса вашего игрока вы можете создать новый класс «это», а затем вызвать его по имени, чтобы получить все характеристики, которые вы ему дали.