Gå til innhold

Tic tac toe - Metode for å sjekke om noen har vunnet.


Anbefalte innlegg

Hei, jeg driver å lager et helt enkelt tic tac toe spill, men jeg sliter litt med å finne en lett metode for å se om det er 3 like.

 

String [] grensesnitt = {"x", "x", "x", "o", "x", "o", "x", "o", "o"}

 

Det som her blir skrevet ut er da

x x x

o x o

x o o

 

Jeg trenger nå en lett metode for å finne ut at det er 3 på rad på toppen der.

Måten jeg bruker nå er:

 

if ( grensesnitt[1] == "x" && grensesnitt[2] == "x" && grensesnitt[3] == "x")

{

System.out.println("Spiller en vinner");

}

 

Denne funker jo, men det er sykt tungvindt å lage en sånn metode for alle 8 mulige kombinasjoner, derfor lurer jeg på om det er noe annen evt lettere.

 

Hadde vært fint om det hadde gått med noe sånn som (grensesnitt[1], grensesnitt[2], grensesnitt[3] == "x")

Altså at du samler alt i et parameter.

 

Bare å komme med lure innspill. På forhånd takk :)

Lenke til kommentar
Videoannonse
Annonse

Nå ville jeg forenkle det ved å bruke tall i stede for tegn, og dobbelt array til å beskrive brettet. Antar du kanskje er tidlig i læringen så måten jeg gjør det på her er kanskje litt over det du har lært? Men er i hvertfall en objektorientert mpte å gjøre det på som jeg anser som relativt enkel.

 

public class TicTacTie {
 public int[][] board = {
  {2, 1, 2},
  {2, 2, 1},
  {2, 1, 1}
   };

 // Returns the winner. (Assumes that there is only 1 winner)
 // Returns 0 if no winners found.
 public int getWinner() {
   for(int x=0; x<3; x++) {
  // Rows
  if(this.isSame(this.board[x][0], this.board[x][1], this.board[x][2]))
    return this.board[x][0];
  // Columns
  if(this.isSame(this.board[0][x], this.board[1][x], this.board[2][x]))
    return this.board[x][0];
   }

   // Diagonals
   if(this.isSame(this.board[0][0], this.board[1][1], this.board[2][2]))
  return this.board[0][0];
   if(this.isSame(this.board[2][0], this.board[1][1], this.board[0][2]))
  return this.board[2][0];
   return 0;
 }

 public boolean isSame(int a, int b, int c) {
   if((a==b) && (a==c))
  return true;
   return false;
 }
}

Endret av etse
Lenke til kommentar

Her er en alternativ måte å gjøre det på (sier _ikke_ at det er lurt å gjøre det slik, hadde bare lyst til å prøve :wee: ):

 


public class TicTacToe {

 public static void main(String[] args) {
   TicTacToe game = new TicTacToe();
   java.util.Scanner scan = new java.util.Scanner(System.in);
   int player = 0;
   do {
     game.printBoard();
     System.out.print("Player " + (player == 0 ? "O" : "X") + ": Enter pos: ");
     String line = scan.nextLine();
     boolean colorPlaced = false;
     if (line.equalsIgnoreCase("A1")) colorPlaced = game.putColor(player, TicTacToe.POS_A1);
     else if (line.equalsIgnoreCase("B1")) colorPlaced = game.putColor(player, TicTacToe.POS_B1);
     else if (line.equalsIgnoreCase("C1")) colorPlaced = game.putColor(player, TicTacToe.POS_C1);
     else if (line.equalsIgnoreCase("A2")) colorPlaced = game.putColor(player, TicTacToe.POS_A2);
     else if (line.equalsIgnoreCase("B2")) colorPlaced = game.putColor(player, TicTacToe.POS_B2);
     else if (line.equalsIgnoreCase("C2")) colorPlaced = game.putColor(player, TicTacToe.POS_C2);
     else if (line.equalsIgnoreCase("A3")) colorPlaced = game.putColor(player, TicTacToe.POS_A3);
     else if (line.equalsIgnoreCase("B3")) colorPlaced = game.putColor(player, TicTacToe.POS_B3);
     else if (line.equalsIgnoreCase("C3")) colorPlaced = game.putColor(player, TicTacToe.POS_C3);
     if (colorPlaced)
       player = 1 - player;
     else
       System.out.println("Please try again");
   } while (game.getWinner() == -1);
   game.printBoard();
   System.out.println("Winner is player " + (game.getWinner() == 0 ? "O" : "X") + "!");
 }

 private final static int POS_A1 = 0400;
 private final static int POS_B1 = 0200;
 private final static int POS_C1 = 0100;
 private final static int POS_A2 = 0040;
 private final static int POS_B2 = 0020;
 private final static int POS_C2 = 0010;
 private final static int POS_A3 = 0004;
 private final static int POS_B3 = 0002;
 private final static int POS_C3 = 0001;
 private final static int[][] POSITIONS = {
   {POS_A1, POS_B1, POS_C1},
   {POS_A2, POS_B2, POS_C2},
   {POS_A3, POS_B3, POS_C3}};

 private final static int WIN_ROW1 = 0700;
 private final static int WIN_ROW2 = 0070;
 private final static int WIN_ROW3 = 0007;
 private final static int WIN_COLA = 0444;
 private final static int WIN_COLB = 0222;
 private final static int WIN_COLC = 0111;
 private final static int WIN_A1B2C3 = 0421;
 private final static int WIN_A3B2C1 = 0124;
 private final static int[] WIN_SOLUTIONS = {
   WIN_ROW1, WIN_ROW2, WIN_ROW3,
   WIN_COLA, WIN_COLB, WIN_COLC,
   WIN_A1B2C3, WIN_A3B2C1};

 private int boardUsedCells = 0000;
 private int boardColors = 0000;

 // Returns the winner. (Assumes that there is only 1 winner)
 // Returns -1 if no winners found.
 public int getWinner() {
   for (int solution : WIN_SOLUTIONS) {
     if ((boardUsedCells & solution) == solution) {
       if ((boardColors & solution) == 0) {
         return 0;
       } else if ((boardColors & solution) == solution) {
         return 1;
       }
     }
   }
   return -1;
 }

 public boolean putColor(int color, int pos) {
   if ((boardUsedCells & pos) == 0) {
     boardUsedCells |= pos;
     if (color != 0) {
       boardColors |= pos;
     }
     return true;
   }
   return false;
 }

 public void printBoard() {
   System.out.println("   | A | B | C |");
   System.out.println("---|---|---|---|");
   int rowNo = 1;
   for (int[] row : POSITIONS) {
     System.out.print(" " + rowNo++ + " |");
     System.out.print(((boardUsedCells & row[0]) == 0 ? "   "
         : (boardColors & row[0]) == 0 ? " O " : " X ") + "|");
     System.out.print(((boardUsedCells & row[1]) == 0 ? "   "
         : (boardColors & row[1]) == 0 ? " O " : " X ") + "|");
     System.out.println(((boardUsedCells & row[2]) == 0 ? "   "
         : (boardColors & row[2]) == 0 ? " O " : " X ") + "|");
     System.out.println("---|---|---|---|");
   }
 }

}

Lenke til kommentar

siden man allerede er litt offtopic, og TS har fått svar kan jeg komme med en litt mer "hacky" løsning da jeg kjeder meg. Forutsetter at spillere er markert med 1 og 2. 0 i feltene hvor det ikke er lagt noen brikker enda.

 

public class TicTacToe {
 public int[][] board = {
  {2, 1, 2},
  {1, 0, 1},
  {2, 1, 1}
   };

 // Returns the winner. (Assumes that there is only 1 winner)
 // Returns 0 if no winners found.
 public int getWinner() {
   int winner = 0;
   for(int x=0; x<3; x++) {
  winner |= this.board[x][0] & this.board[x][1] & this.board[x][2];
  winner |= this.board[0][x] & this.board[1][x] & this.board[2][x];
   }
   winner |= this.board[0][0] & this.board[1][1] & this.board[2][2];
   winner |= this.board[2][0] & this.board[1][1] & this.board[0][2];
   return winner;
 }
}

 

Her vil man etter hvert trekk kalle "getWinner()" og om den returnerer null fortsetter man å spille, er den 1 eller 2 har denne spilleren vunnet.

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...