Tic Tac Toe in Java Konsole

Status
Nicht offen für weitere Antworten.

Javahs

Mitglied
Hallo,
ich weiß, dass man sowas hier eigentlich net fragen soll, aber hat zufällig jemand ein fertiges Programm in Java für das Spiel Tic Tac Toe ohne Applet, wo man nur Spieler vs. Spieler machen kann ohne Computer?

Mit freundlichen Grüßen
javahs
 

L-ectron-X

Gesperrter Benutzer
Hab mal biss'l gekramt und in meinen ganz alten Programmen das gefunden:
Code:
import java.io.*;

class TicTacToe
{
  char[][] board;
  char winner;
  char nextPlayer;
  BufferedReader keyIn;

  public TicTacToe(BufferedReader keyInput)
  {

     nextPlayer = 'x';
     keyIn=keyInput;
     board=new char [3][3];
     board[0][0]=' ';
     board[0][1]=' ';
     board[0][2]=' ';
     board[1][0]=' ';
     board[1][1]=' ';
     board[1][2]=' ';
     board[2][0]=' ';
     board[2][1]=' ';
     board[2][2]=' ';
  }

  // Display the current status of the board on the
  // screen, using hyphens (-) for horizontal lines
  // and pipes (|) for vertical lines.
  public void displayBoard()
  {
     System.out.println(board[0][0]+"|"+board[0][1]+"|"+board[0][2]);
     System.out.println("-----");
     System.out.println(board[1][0]+"|"+board[1][1]+"|"+board[1][2]);
     System.out.println("-----");
     System.out.println(board[2][0]+"|"+board[2][1]+"|"+board[2][2]);
     System.out.println("");
  }

  public void turn() throws Exception
  {
     System.out.println("It is now "+nextPlayer+"'s turn");
     int validIn = 1; //was 0
     int row;
     int column;

     do
     {
         System.out.println("Please enter your move in the form row,column.");
         System.out.println("So 0,0 would be the top left, and 0,2 would be the top right.\n");
         String input = keyIn.readLine();
         //int row;
         row = Integer.parseInt(input.substring(0,1));
         //int column;
         column= Integer.parseInt(input.substring(2,3));

         //check if the entered value is valid
         if (( row < 0  ||  row > 2 ) && ( column < 0 || column > 2 ))
         //if ((( row >= 0 ) && ( row <= 2 )) && (( column >= 0) && (column <= 2)))
         {
             validIn=0;
             System.out.println("Invalid entry: ("+row+""+input.substring(1,2)+""+column+") row and column must both be between 0 and 2 (inclusive).");
             System.out.println("Please try again.\n");
         }

         if ( board[row][column]!=' ' ) //Specified position is already occupied
         {
            validIn = 0;
            System.out.println("Invalid entry: Row "+row+" at Column "+column+" already contains: "+board[row][column]+" ");
         }

         if ( !input.substring(1,2).equals(",") ) //No comma in input
          //( input.substring(1,2) != ',') ) //not right!
         {
            validIn = 0;
            System.out.println("Invalid entry: comma needed. Please try again.");
         }
      }

      while( validIn == 0 );

         board[row][column] = nextPlayer;
         System.out.println("");

         if (nextPlayer == 'x')
           nextPlayer = 'o';

         else
           nextPlayer = 'x';
  }

  public char winner()
  {
      //int won = 0;

      for (int i = 0; i < 3; i++)
      {
         if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))
         {
            //won = 0;
            winner = board[0][i];
         }
      }

      for (int j = 0; j < 3; j++)
      {
         if ((board[j][0] == board[j][1]) && (board[j][1] == board[j][2]))
         {
            //won = 0;
            winner = board[j][0];
         }
      }
      return winner;
   }

  public static void main(String[] args) throws Exception
  {
     char winningPlayer;
     BufferedReader keyboard = new BufferedReader(new
                                   InputStreamReader(System.in));

     TicTacToe game = new TicTacToe(keyboard);

     game.displayBoard();
     do
     {
        game.turn();
        game.displayBoard();
     }

     while(game.winner() == ' ');

        winningPlayer = game.winner();

        if (winningPlayer == '?')
           System.out.println("Nobody won. Please play again.");

        else
           System.out.println("Congratulations, " + winningPlayer + " YOU WON!");
  }

}
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben