嗨檢查什麼,我沒到目前爲止,我對第二個方法 http://pastebin.com/XDsp6ze7
// Family name, Given name:
// Student number:
// Course: IT1 1120
// Assignment: 4
import java.util.Scanner;
import java.io.IOException;
public class A4_8044047{
// main method. DO NOT MODIFY
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to Memory Game");
int board_side;
//this loop obtains the board size, or more specifically
// the length of the side of the board
do{
System.out.println("\n For 2x2 board game press 2"+
"\n For 4x4 board game press 4"+
"\n For 6x6 board game press 6");
board_side=keyboard.nextInt();
}while(board_side!=2 && board_side!=4 && board_side!=6);
char[][] board = createBoard(board_side);
// a call the the shuffle method
shuffleBoard(board);
// a call to the game playing method
//playGame(board);
}
// The following method should shuffle the input 2D array caled board
public static void shuffleBoard(char[][] board)
{
// This creates a 1D array whose size is equal to the size of the board
int N = board.length*board.length;
char[] board1D = new char[N];
// Copy the elements of 2D array into that 1D array here
int a = 0;
for (int b=0;b<board.length;b++)
{
for (int c=0;c<board.length;c++)
{
board1D[a] = board[b][c];
a++;
}
}
// Shuffle 1D array here
for (int d=0;d<board1D.length;d++)
{
int index=(int)(Math.random()* board1D.length);
char temp=board1D[d];
board1D[d]=board1D[index];
board1D[index]=temp;
}
for(int row2=0;row2<board1D.length;row2++)
{
//System.out.print(board1D[row2] + " ");
}
//Copy shuffled 1D array back into 2D array, i.e., back to the board
int M;
if (N==4)
{
M=2;
}
else if (N==16)
{
M=4;
}
else
{
M=6;
}
int count=0;
for (int h=0;h<M;h++)
{
for (int k=0;k<M;k++)
{
if (count==board1D.length)
{
break;
}
board[h][k]=board1D[count];
count++;
}
}
/*for(int row2=0;row2<M;row2++)
{
for(int col2=0;col2<M;col2++)
{
System.out.print(board[row2][col2]+ " ");
}
System.out.println();
}*/
}
// a game playing method
public static void playGame(char[][] board)
{
Scanner keyboard = new Scanner(System.in);
// this createst a 2D array indicating what locations are paired, i.e., discovered
// at the begining none are, so default initializaiton to false is ok
boolean[][]discovered=new boolean[board.length][board[0].length];;
// the code for your game playing goes here
Still working on this part any help would be nice email: [email protected]
}
// createBoard method. DO NOT MODIFY!
/* this method, createBoard, creates the board filled with letters of alphabet,
where each letter appears exactly 2 times
e.g., for 4 x 4, the returned board would look like:
A B C D
E F G H
A B C D
E F G H */
public static char[][] createBoard(int side)
{
char[][] tmp = new char[side][side];
int letter_count=0;
for (int row = 0; row < tmp.length/2; row++){
for(int col = 0; col < tmp[row].length; col++)
{
tmp[row][col]=(char)('A'+letter_count);
tmp[row+tmp.length/2 ][col]=tmp[row][col];
letter_count++;
}
}
return tmp;
}
// waitForPlayer method. Do not modify!
public static void waitForPlayer()
{
System.out.print("Press enter to continue");
try {
System.in.read();
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
}
雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Sasa