2014-11-09 100 views
0

我需要編寫一個程序:模擬一個名爲Ognib的雙人遊戲。在Ognib中,每個玩家都會得到一個5x5的格子,他們會在其中放置數字令牌。令牌用1到100之間的整數進行編號,它們都是不同的,並按升序排列。玩家不能看到他們的對手的網格 - 只有他們自己的網格。一次一個,每個玩家「調用」一個整數,並且如果任何玩家在他們的網格上有這個令牌,他們將令牌面朝下放在他們的網格上。遊戲的目標是讓玩家在對手所做的事情之前,將所有代幣連續或者在一列或者兩個對角線中的任何一個朝下。
使用一維數組。陣列,Ognib遊戲

我在我的程序中有一些問題, 首先我不知道如何在一個程序中使用兩個類,實際上我的程序在一個類中被壓縮,我覺得有點混亂。第二,如果玩家有一個0的對角線,我的獲勝條件是行不通的......而我不知道爲什麼...... 第三,有25輪的遊戲輪的限制,它是陣列,但我不知道如何改變這...

import java.util.Random; 
import java.util.Scanner; 

public class Ognib { 
    public static void main (String[] args) { 
     Scanner keyboard = new Scanner (System.in); 
      final int ROWS = 5; 
      final int COLUMNS = 5; 
      final int LENGTH = ROWS*COLUMNS; 
      final int MIN_VALUE = 1; // represents the minimum value in the board 
      final int MAX_VALUE = 100; // represents the maximun value in the board 
      int number = 0; 
      int[] board1 = new int[LENGTH]; // represents the board as a 1-D array 

      Random rand = new Random(); 
      int randomNumber = 0; // an integer to be chosen at random 
      boolean isDuplicate; // to indicate is randomNumber has already been picked or not 

      // for every cell i in the board 
      for (int i = 0; i < board1.length; ++i) 
      { 
       do 
       {  // Generate a random number between MAX_VALUE and MIN_VALUE inclusive 
         randomNumber = rand.nextInt((MAX_VALUE - MIN_VALUE) + 1) + MIN_VALUE; 

         isDuplicate = false; // initialy, let's assume this is not a duplicate 

         // check if this random value has already been inserted in the array in a previous cell 
         for (int j = 0; j < i; ++j) { 
          if (board1[j] == randomNumber) { 
            isDuplicate = true; 
        } 
      } 
       } 
       while (isDuplicate); // pick a new random number until we pick a non-duplicate 
       board1[i] = randomNumber; // this random number is not a duplicate, assign it to the ith cell 
      } 
      int indexOfMin; 
      int temp; // for every element (except the last)   
      for (int index = 0; index < board1.length-1; index++) { 
      indexOfMin = index; // find the index of the smallest element between index and the last  
      for (int scan = index+1; scan < board1.length; scan++)  
       if (board1[scan] < board1[indexOfMin])   
       indexOfMin = scan;  // Swap the smallest element with the one at position ‘index’  
      temp = board1[indexOfMin];  
      board1[indexOfMin] = board1[index];  
      board1[index] = temp; }  

      int[] board2 = new int[LENGTH]; 
      for (int i = 0; i < board2.length; ++i) 
      { 
       do 
       {  // Generate a random number between MAX_VALUE and MIN_VALUE inclusive 
         randomNumber = rand.nextInt((MAX_VALUE - MIN_VALUE) + 1) + MIN_VALUE; 

         isDuplicate = false; // initialy, let's assume this is not a duplicate 

         // check if this random value has already been inserted in the array in a previous cell 
         for (int j = 0; j < i; ++j) { 
          if (board2[j] == randomNumber) { 
            isDuplicate = true; 
        } 
      } 
       } 
       while (isDuplicate); // pick a new random number until we pick a non-duplicate 
       board2[i] = randomNumber; // this random number is not a duplicate, assign it to the ith cell 
      } 
      for (int index = 0; index < board2.length-1; index++) { 
       indexOfMin = index; // find the index of the smallest element between index and the last  
       for (int scan = index+1; scan < board2.length; scan++)  
        if (board2[scan] < board2[indexOfMin])   
        indexOfMin = scan;  // Swap the smallest element with the one at position ‘index’  
       temp = board2[indexOfMin];  
       board2[indexOfMin] = board2[index];  
       board2[index] = temp; } 

      boolean win; 
      // the array now contains all unique random values 
      System.out.print("Enter your name: "); 
      String name = keyboard.next(); 

     do 
     { 
      win=false; 
      System.out.println("Player Name: " + name); 
      System.out.println("====================================================================================================="); 
      for (int h = 0; h < board1.length && h < board2.length; ++h) { 
       if ((h % 5)==0 && (h != 0)) 
       System.out.println ("|"); 
       System.out.print ("|"); 
       System.out.print("  "+ board1[h] + "  ");} 
      System.out.println("|"); 
      System.out.print("====================================================================================================="); 
      System.out.println(); 
      for (int e = 1 ; e<=3 ; e++) { 
       randomNumber = rand.nextInt((MAX_VALUE - MIN_VALUE) + 1) + MIN_VALUE ; 
       System.out.println ("Computer's Value: " + randomNumber); 
      for (int k = 0; k < board1.length && k < board2.length; ++k){ 
       if (board1[k] == randomNumber) 
        board1[k] = 0; 
       if (board2[k] == randomNumber) 
        board2[k] = 0; } 
       } 
      for (int f=1 ; f<=1 ; f++){ 
      System.out.print (name + "'s Value: "); 
      number = keyboard.nextInt(); 
      for (int j = 0; j < board1.length && j < board2.length; ++j){ 
       if (board1[j]== number) 
        board1[j] = 0; 
       if (board2[j] == number) 
        board2[j] = 0; } 
      System.out.println(); } 
      if ((board1[1]==0 && board1[7]==0 && board1[13]==0 && board1[19]==0 && board1[25]==0) || 
       (board1[5]==0 && board1[9]==0 && board1[13]==0 && board1[17]==0 && board1[21]==0) || 
       (board2[1]==0 && board2[7]==0 && board2[13]==0 && board2[19]==0 && board2[25]==0) || 
       (board2[5]==0 && board2[9]==0 && board2[13]==0 && board2[17]==0 && board2[21]==0)){ 
       System.out.println("Diagonal win detected..."); 
       win=true; 
       break; } 
      if (board1[1]==0 && board1[6]==0 && board1[11]==0 && board1[16]==0 && board1[21]==0 || 
       board1[2]==0 && board1[7]==0 && board1[12]==0 && board1[17]==0 && board1[22]==0 || 
       board1[3]==0 && board1[8]==0 && board1[13]==0 && board1[18]==0 && board1[23]==0 || 
       board1[4]==0 && board1[9]==0 && board1[14]==0 && board1[19]==0 && board1[24]==0 || 
       board1[5]==0 && board1[10]==0 && board1[15]==0 && board1[20]==0 && board1[25]==0|| 
       board2[1]==0 && board2[6]==0 && board2[11]==0 && board2[16]==0 && board2[21]==0 || 
       board2[2]==0 && board2[7]==0 && board2[12]==0 && board2[17]==0 && board2[22]==0 || 
       board2[3]==0 && board2[8]==0 && board2[13]==0 && board2[18]==0 && board2[23]==0 || 
       board2[4]==0 && board2[9]==0 && board2[14]==0 && board2[19]==0 && board2[24]==0 || 
       board2[5]==0 && board2[10]==0 && board2[15]==0 && board2[20]==0 && board2[25]==0){ 
       System.out.println("Colum win detected..."); 
       win = true; 
       break;} 
      if (board1[1]==0 && board1[2]==0 && board1[3]==0 && board1[4]==0 && board1[5]==0 || 
       board1[6]==0 && board1[7]==0 && board1[8]==0 && board1[9]==0 && board1[10]==0 || 
       board1[11]==0 && board1[12]==0 && board1[13]==0 && board1[14]==0 && board1[15]==0 || 
       board1[16]==0 && board1[17]==0 && board1[18]==0 && board1[19]==0 && board1[20]==0 || 
       board1[21]==0 && board1[22]==0 && board1[23]==0 && board1[24]==0 && board1[25]==0|| 
       board2[1]==0 && board2[2]==0 && board2[3]==0 && board2[4]==0 && board2[5]==0 || 
       board2[6]==0 && board2[7]==0 && board2[8]==0 && board2[9]==0 && board2[10]==0 || 
       board2[11]==0 && board2[12]==0 && board2[13]==0 && board2[14]==0 && board2[15]==0 || 
       board2[16]==0 && board2[17]==0 && board2[18]==0 && board2[19]==0 && board2[20]==0 || 
       board2[21]==0 && board2[22]==0 && board2[23]==0 && board2[24]==0 && board2[25]==0){ 
       System.out.println("Row win detected..."); 
       win=true; 
       break; } 
      } 
     while (!win); 

     if (win=true) 
     System.out.println("===Final Results==="); 
     System.out.println("Player Name: " + name); 
     for (int h = 0; h < board1.length; ++h) { 
      if ((h % 5)==0 && (h != 0)) 
      System.out.println ("|"); 
      System.out.print ("|"); 
      System.out.print("  "+ board1[h] + "  "); } 
     System.out.println(); 
     if (board1[1]==0 && board1[6]==0 && board1[11]==0 && board1[16]==0 && board1[21]==0 || 
      board1[2]==0 && board1[7]==0 && board1[12]==0 && board1[17]==0 && board1[22]==0 || 
      board1[3]==0 && board1[8]==0 && board1[13]==0 && board1[18]==0 && board1[23]==0 || 
      board1[4]==0 && board1[9]==0 && board1[14]==0 && board1[19]==0 && board1[24]==0 || 
      board1[5]==0 && board1[10]==0 && board1[15]==0 && board1[20]==0 && board1[25]==0|| 
      board1[1]==0 && board1[2]==0 && board1[3]==0 && board1[4]==0 && board1[5]==0 || 
      board1[6]==0 && board1[7]==0 && board1[8]==0 && board1[9]==0 && board1[10]==0 || 
      board1[11]==0 && board1[12]==0 && board1[13]==0 && board1[14]==0 && board1[15]==0 || 
      board1[16]==0 && board1[17]==0 && board1[18]==0 && board1[19]==0 && board1[20]==0 || 
      board1[21]==0 && board1[22]==0 && board1[23]==0 && board1[24]==0 && board1[25]==0|| 
      board1[1]==0 && board1[7]==0 && board1[13]==0 && board1[19]==0 && board1[25]==0 || 
      board1[5]==0 && board1[9]==0 && board1[13]==0 && board1[17]==0 && board1[21]==0){ 
      System.out.println(name + "has a winning card!"); 
      System.out.println();} 
     System.out.println("Player Name: Computer"); 
     for (int h = 0; h < board2.length; ++h) { 
      if ((h % 5)==0 && (h != 0)) 
      System.out.println ("|"); 
      System.out.print ("|"); 
      System.out.print("  "+ board2[h] + "  "); } 
     System.out.println(); 
     if (board2[1]==0 && board2[6]==0 && board2[11]==0 && board2[16]==0 && board2[21]==0 || 
      board2[2]==0 && board2[7]==0 && board2[12]==0 && board2[17]==0 && board2[22]==0 || 
      board2[3]==0 && board2[8]==0 && board2[13]==0 && board2[18]==0 && board2[23]==0 || 
      board2[4]==0 && board2[9]==0 && board2[14]==0 && board2[19]==0 && board2[24]==0 || 
      board2[5]==0 && board2[10]==0 && board2[15]==0 && board2[20]==0 && board2[25]==0|| 
      board2[1]==0 && board2[2]==0 && board2[3]==0 && board2[4]==0 && board2[5]==0 || 
      board2[6]==0 && board2[7]==0 && board2[8]==0 && board2[9]==0 && board2[10]==0 || 
      board2[11]==0 && board2[12]==0 && board2[13]==0 && board2[14]==0 && board2[15]==0 || 
      board2[16]==0 && board2[17]==0 && board2[18]==0 && board2[19]==0 && board2[20]==0 || 
      board2[21]==0 && board2[22]==0 && board2[23]==0 && board2[24]==0 && board2[25]==0 || 
      board2[1]==0 && board2[7]==0 && board2[13]==0 && board2[19]==0 && board2[25]==0 || 
      board2[5]==0 && board2[9]==0 && board2[13]==0 && board2[17]==0 && board2[21]==0) { 
      System.out.println("Computer has a winning card!"); } 
     } 

    } 

謝謝!

回答

0

數組的索引從0開始。因此,無論您在程序中寫入的索引是多少,都會減少1。數組索引從0開始不是1.用每個索引減1來代替每個索引,它應該可以正常工作。讓我知道 ! :)

0

索引應該從0開始,而不是1