2011-01-24 38 views
0

我正在做一个TicTacToe计划,唯一缺失的部分是让用户选择是否退出游戏或重放。我找不到一种“回归”游戏的方式。如何返回游戏?完成游戏后

import java.io.*; 

public class Expierment 
{ 
     static char c1 [] = new char[10]; 
     static char c2 [] = new char[10]; 
     static char c3 [] = new char[10]; 
     static char p1; 
     static char p2; 
     static boolean gameOver = false; 
     public static void main(String args[]) 
     { 
      int counter = 0; 
      int p1Wins = 0; 
      int p2Wins = 0; 
      int r1 = 0; 
      int r2 = 0; 
      int r3 = 0; 
      int r4 = 0; 
      int r5 = 0; 
      int r6 = 0; 
      int r7 = 0; 
      int r8 = 0; 
      int r9 = 0; 
      int pick1 = 0; 
      int pick2 = 0; 
      int pick3 = 0; 
      int pick4 = 0; 
      int pick5 = 0; 
      int pick6 = 0; 
      int pick7 = 0; 
      int pick8 = 0; 
      int pick9 = 0; 
      char turn = 'X'; 
      int choice = menu(); 
      switch(choice) 
      { 
       case 1: 
        System.out.println("The game is called 'Tic-Tac-Toe', you should have known it. If you don't, search it.") ; 
       case 2: 
        gameOver = false; 
        break; 
       case 3: 
        System.out.println("\nSee you next time !!"); 
        return; 
       default: 
        System.out.println("\nYou hit the wrong key......\n"); 
        return; 
      }//end of switch 
      System.out.println("\nPlayer 1 initials ?"); 
      String n1 = GCS(); 
      p1 = n1.charAt(0); 
      System.out.println("\nPlayer 2 initials ?"); 
      String n2 = GCS(); 
      p2 = n2.charAt(0); 
      c1[2]='1'; 
      c2[2]='2'; 
      c3[2]='3'; 
      c1[1]='4'; 
      c2[1]='5'; 
      c3[1]='6'; 
      c1[0]='7'; 
      c2[0]='8'; 
      c3[0]='9'; 
     printBoard(); 
     while(gameOver!=true) 
     { 
      System.out.println("Which spot ?"); 
      int pick = Integer. parseInt(GCS()); 
       switch (pick) 
      { 
       case 1: 
        if (r1<1) 
       { 
        c1[2] = turn; 
        r1++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break;     
       case 2: 
        if (r2<1) 
       { 
        c2[2] = turn; 
        r2++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break;     
       case 3: 
        if (r3<1) 
       { 
        c3[2] = turn; 
        r3++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       case 4: 
        if (r4<1) 
       { 
        c1[1] = turn; 
        r4++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       case 5: 
        if (r5<1) 
       { 
        c2[1] = turn; 
        r5++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       case 6: 
        if (r6<1) 
       { 
        c3[1] = turn; 
        r6++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       case 7: 
        if (r7<1) 
       { 
        c1[0] = turn; 
        r7++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       case 8: 
        if (r8<1) 
       { 
        c2[0] = turn; 
        r8++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       case 9: 
        if (r9<1) 
       { 
        c3[0] = turn; 
        r9++; 
       } 
       else 
       { 
        System.out.println("That column is full, pick another.\n"); 
        continue; 
       } 
        break; 
       default: 
        System.out.println("Seriously?! Pick a possible spot.\n"); 
        continue; 
       }//end of switch 
      if (turn=='X') turn = 'O'; 
      else turn = 'X'; 
      printBoard(); 
      if (checkWinner()) 
      { 
       while(gameOver==true) 
       { 
        int Echoice = EGM(); 
        switch(Echoice) 
        { 
         case 1: 
          System.out.println("The game is called 'Tic-Tac-Toe', you should have known it. If you don't, search it.") ; 
         case 2: 
          gameOver = false; 
          menu(); 

          break; 
         case 3: 
          System.out.println("\nSee you next time !!"); 
          return; 
         default: 
          System.out.println("\nYou hit the wrong key......\n"); 
          return; 
        }//end of switch 
       }//end of while true 
       return; 
      } 
      counter ++; 
      if (counter==9) 
      { 
       System.out.println("\n\nYou tied.\n"); 
       return; 
      } 
     }//end of while not true 
    }//end of main 

    public static boolean checkWinner() 
    { 
      for (int k=0; k<2; k++) 
     { 
      if ((c1[k]!=' ')&&(c1[k]==c2[k])&&(c1[k]==c3[k])) 
      { 
       System.out.println("\nYo " + c1[k] + " is the winner!\n"); 
       gameOver=true; 
       return true; 
      }//checks column 1-3 horizontally 
     }//end of horizontal check 

     for (int m=0; m<2; m++) 
     { 
      if((c1[m]!=' ')&&(c1[m]==c1[m+1])&&(c1[m+1]==c1[m+2])&&(c1[m]==c1[m+2])) 
      { 
       System.out.println("\nYo " + c1[m] + " is the winner!\n"); 
       gameOver=true; 
       return true; 
      }//checks column 1 vertically 
      if((c2[m]!=' ')&&(c2[m]==c2[m+1])&&(c2[m+1]==c2[m+2])&&(c2[m]==c2[m+2])) 
      { 
       System.out.println("\nYo " + c2[m] + " is the winner!\n"); 
       gameOver=true; 
       return true; 
      }//checks column 2 vertically 
      if((c3[m]!=' ')&&(c3[m]==c3[m+1])&&(c3[m+1]==c3[m+2])&&(c3[m]==c1[m+2])) 
      { 
       System.out.println("\nYo " + c3[m] + " is the winner!\n"); 
       gameOver=true; 
       return true; 
      }//checks column 3 vertically 
       if ((c1[m]!=' ')&&(c1[m]==c2[m+1])&&(c1[m]==c3[m+2])) 
      { 
       System.out.println("\nYo " + c1[m] + " is the winner!\n"); 
       gameOver=true; 
       return true; 
      }//checks upward diagonal 
       if ((c3[m]!=' ')&&(c3[m]==c2[m+1])&&(c3[m]==c1[m+2])) 
      { 
       System.out.println("\nYo " + c1[m] + " is the winner!\n"); 
       gameOver=true; 
       return true; 
      } 
     }//end of vertical check 
       return false; 
    }//end of checkWinner 

    public static void printBoard() 
    { 
     System.out.println("_______"); 
     for (int j = 2; j > -1; j--) 
     { 
      System.out.println("|" + c1[j] + "|" + c2[j] + "|" + c3[j] + "|"); 
      System.out.println("-------"); 
     } 
    }//end of printBoard 

    public static int menu() 
    { 
     System.out.println("Tic-Tac-Toe ~ Main Menu\n\n1. Instructions\n2. Play a 1 player game"+"\n3. Exit\n"); 
     int selection = Integer.parseInt(GCS()); 
     return selection; 
    }//end of menu 

    public static int EGM() 
    { 
     System.out.println("Tic-Tac-Toe ~ End of Game Menu\n\n1. Instructions\n2. Play again"+"\n3. Exit\n"); 
     int Eselection = Integer.parseInt(GCS()); 
     return Eselection; 
    } 

    public static String GCS() 
    { 
     int noMoreInput=-1; 
     char enterKeyHit='\n'; 
     int InputChar; 
     StringBuffer InputBuffer = new StringBuffer(100); 

     try 
     { 
      InputChar=System.in.read(); 
      while(InputChar != noMoreInput) 
      { 
       if((char)InputChar !=enterKeyHit) 
       { 
        InputBuffer.append((char)InputChar); 
       } 
       else 
       { 
        InputBuffer.setLength(InputBuffer.length()-1); 
        break; 
       } 
       InputChar=System.in.read(); 
      } 
     } 
     catch (IOException IOX) 
     { 
      System.err.println(IOX); 
     } 
     return InputBuffer.toString(); 
     }//end of GCS 
}//end of public class 
+0

独立于您的问题,您可能需要考虑重写此代码以消除对pick1,pick2,pick9变量的需求。如果你发现自己编写了一个看起来像这样的变量集合,那么你几乎总是可以通过使用一个数组来重写它,如`int [] pick = new int [9]`。这允许您以`pick [0]`,...,`pick [8]`的形式访问所有值,但更重要的是,它可以让您以编程方式生成索引。如果你愿意花时间,这可以让你将`switch`语句压缩成只有几行代码。 – templatetypedef 2011-01-24 22:27:18

+0

请修复格式 – 2011-01-24 22:28:24

+0

一些提示,r1-r9,pick1-pick9,使用数组来代替,与你的cs一样,将它们改为一个字符串数组,然后像这样初始化它们c [0] =“741”; – hhafez 2011-01-24 22:28:53

回答

2

你真的应该得到一些代码出来的main功能。

具体来说,我会把整个游戏循环放在一个单独的函数中,可能被称为playGame(),它包含玩游戏,检查赢者等逻辑,并且返回赢家或者打印赢家并返回无效。

然后主要功能可以在循环中呼叫playGame(),并在循环结束时询问用户他/她是否想再次播放。

一般来说,你想让每个功能都做一个逻辑任务。你在移动checkWinner时做得很好,现在可以和其他一些代码一样。

如果您需要关于“询问用户再次播放”的帮助,请留下评论,我会进行编辑以解决该问题。

1
quick and dirty pseudo-code - not modular 

do {  
    //everything in your main goes here 
    . 
    . 
    .  
    playAgain = prompt the user 
} while(playAgain); 
0

用更简洁的主要方法,事情会更加清晰。真正快速和肮脏的方法是将主要方法中的所有内容复制到新方法中,并从用户选择开始新游戏时调用该方法。

1

比do-while方法更脏。最终会导致堆栈溢出。

//your current main method 
boolean playAgain = prompt the user 
if(playAgain){ 
    main(args); 
} 
1

随着当前的程序布局,没有“干净”的方式来实现。这里有一些建设性的批评:

  • 你的主要方法应该只是一个引导程序

你只应该在你main方法来初始化程序。因此,尽量有方法只做的事情。你现在的设计有相同方法中的游戏菜单和游戏主循环。

你的游戏循环可能看起来像这样:

while still playing 
    read input from user 
    if game is active 
     process game input 
     update game 
    else 
     process menu input 
     update menu 

这样,你只需要在SWITH状态game is active在菜单或游戏。等等。

GCS方法太复杂,只是将其替换为:

Scanner scanner = new Scanner();  // put this somewhere at the class level (so it is reusable) 
... 
String input = scanner.nextLine(); // put this somewhere in a method reading an input 
  • 可变滥用

而不是初始化许多变量,可能(像其他人一样建议),你可以使用数组,或更具体地说是一个2维数组。

int grid[][] = new int[3][3]; 
// grid[0][0] points to the top-left cell, grid[2][2] points to the bottom right one 

或者你可以使用一个int来存储一切;代表您的网格作为一个bit array

int grid = 0; // empty grid 
... 
// set player move 
grid |= (1 << (y*3)+x) << PLAYER_OFFSET; // PLAYER_OFFSET: 0=player 1, 16=player 2 
// reset (clear) player move 
grid &= ~((1 << (y*3)+x) << PLAYER_OFFSET); // ... 
// check if player move is set 
boolean isSet = (grid >> ((1 << (y*3)+x) << PLAYER_OFFSET)) && 1; // ... 

为什么位列很酷?因为来检查,如果玩家赢了,你不需要花哨for..loop和其他复杂的算法,只是验证对一个成功的模式...:

int pattern = 273; // test for diagonal from [0,0] to [2,2], or bits "100 010 001" 
boolean isWinning = (grid && (pattern << PLAYER_OFFSET)) != 0; 

这就是它!


此外,使用09可能是好的,以确定网格单元,然而作为输入值不是直观的球员。例如,许多游戏(国际象棋,跳棋,LOA等)和应用程序(Excel,Calc等)正在使用Algebraic chess notation系统。转换xy中的符号非常简单。例如:

boolean cellPlayed = false; 
while (!cellPlayed) { 
    String cellStr = scanner.readLine().toLower(); // ex: "b2" for the center cell 
    try { 
     int gridx = cellStr.charAt(0) - 'a'; // ex: for "b2" should return 1 
     int gridy = cellStr.charAt(1) - '1'; // ex: for "b2" should return 1 

     grid(gridx][gridy] = playerValue;  // 1 for "player 1" and 2 for "player 2" 
     cellPlayed = true; 
    } catch (Exception e) { 
     System.out.println("Error! Invalid input"); 
    } 
} 

不要灰心!我们都从某个地方开始! :)