2016-12-17 10 views
-1

私は2次元配列を使用してJavaメモリゲームに取り組んでいます。数字の4×4グリッドがあります。各ターンでプレイヤーは2つの数字を選び、その2つのアレイセルを裏返しにして表示します。色が一致しない場合、カードが裏返しになり、プレイヤーは再び移動しなければなりません。これまで私はゲームの仕組みを作りました。配列はそこにあり、プレイヤーはセルを裏返すことができますが、コードは最初の試行の後に停止します。私はそれを変更して、プレイヤーがすべてのカードに首尾よくマッチするまでプログラムを実行し続けるようにする必要があります。さらに、プレイヤーが2枚のカードとマッチした場合、それらのカードは元に戻りません。私は2つのクラスを持っています:カードとメモリゲーム。ここにあります:進行中のゲームを作るには?

public class Card{ 
    boolean showing; // true or false, indicates that the card is flipped 
    String back;  //hidden symbol on the back of the card 
    int front;  //integer on the front of the card 

    public Card(String theBack, int theFront) 
    { 
     showing = false; 
     back = theBack; 
     front = theFront; 
    } 

    // 
    public void showCard() 
    { 
     if(showing) 
      System.out.print(String.format(back)); //if showing is true, it shows the hidden symbol 
     else 
      System.out.print(String.format("["+front+"]")); 
    } 

    public void setShowingStatus() 
    { 
     if(showing) 
      showing = false; 
     else 
      showing = true; 
    } 
} 

これはメモリゲームクラスです。 playGame()メソッドとchoosePairofCards()メソッドは、私が完了するのに必要なメソッドです。

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

public class MemoryGame{ 
    //Declaration 
    private Card[][] board; 
    private String[] words = {"Blue", "Blue","Red","Red","Green","Green","Yellow","Yellow","Pink","Pink","Black","Black","Brown", "Brown","White","White"}; 
    private Random r; 
    private Scanner reader; 

    MemoryGame(){ 
     r = new Random(); 
     reader = new Scanner(System.in); 
     board = new Card[4][4]; //Creates the 4x4 array that can hold Card objects 
     shuffle(); 
     setCells(); 
     printCells(); 
     playGame(); 
    } 

    public void shuffle(){ 
     // Has a loop that goes throught the array of words and chooses a random position and swaps the words 
     // For loop position to random position 
     // Words array is shuffled 
     for(int a=0; a<words.length; a++){ 
      int pos = r.nextInt(words.length); 
      String temp = words[a]; 
      words[a] = words[pos]; 
      words[pos] = temp; 
     } 
    } 

    public void setCells(){ 
     //Designates the words 
     int a = 0; 
     for (int row = 0; row<board.length; row++){ 
      for (int col = 0; col<board[0].length; col++){ 
       { 
        board[row][col] = new Card(words[a], a); //Creates new Card that pulls words from Shuffled array 
        a++; 
       } 
      } 
     } 
    } 

    public void printCells(){ 
     Card aCard; //Local variable = aCard, which is a type Card 
     for (int row = 0; row<board.length; row++){ 
      for(int col = 0; col<board[0].length; col++){ 
       aCard = board[row][col]; 
       aCard.showCard(); 
      } 
      System.out.println(); 
     } 
    } 

    public void playGame(){ 
     //INCOMPLETE 
     choosePairOfCards(); 
    } 

    public void choosePairOfCards(){ 

     int cardChoice, row1, col1, row2, col2; 
     System.out.println(); 
     System.out.println("Enter the number on the card."); 
     System.out.println("First Card Choice? >"); 
     cardChoice = getInputAsInt(); 
     row1 = cardChoice/4; 
     col1 = cardChoice % 4; 
     board[row1][col1].setShowingStatus(); 

     System.out.print("Second Card Choice? >"); 
     cardChoice = getInputAsInt(); 
     row2 = cardChoice/4; 
     col2 = cardChoice % 4; 
     board[row2][col2].setShowingStatus(); 

     System.out.print('\u000C'); //clears the screen 

     printCells(); // INCOMPLETE 
     // check to see if the "cards" match 
     //if they don't then turn print game with cards turned around 
    } 

    public int getInputAsInt(){ 
     String temp = reader.nextLine(); 
     return Integer.parseInt(temp); //Returns a String containing integer, into an int value 
    } 
} 
+1

'INCOMPLETE' 'playGame()'を完了する必要があるかもしれませんか?今は1回のみターンを呼びます。たぶん[while-loops](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)を参照してください。 – n247s

+0

@ n247s私は、作業が必要なものを示すために不完全を書いていました。 whileループは私が助けを必要とするものです – Mrora

答えて

0

無限のランニングゲームを実装するには、単純にwhileループを使用できます。そのループの中であなたは単に回転し続けます。ゲームが終了した場合は、ループを停止してプログラムをフィンランド語にします。例えば、あなたがmethid isGameFinnishedを持っていると想像してください。これは、gamdがfinnishedであればtrueを返し、falseであればfalseを返します。

while(true) 
{ 
    choosePairOfCards(); 
    if(isGameFinnished()) 
     break; 
} 

上記の例は、ループからアクティブに外れています。あなたは、単にabovrのようなブール値をチェックすることができたとき、あなたはもちろん、私は、これは正しい方向にあなたを助け願ってい

while(!isGameFinnished()) 
{ 
    choosePairOfCards(); 
} 

を行うことができます。

関連する問題