9 Juni 2013

Java Domino Game sederhana




Buat Project untuk client server di netbeans, seperti berikut, :




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domino;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Rendy
 */
public class Domino {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
ArrayList<int []> tiles = new ArrayList<int []>();
Random gen = new Random();

for(int i=0;i<7;i++){
for(int j=0;j<7;j++){
tiles.add(new int[]{i,j});
System.out.print("("+ i + ", " + j + ")");
}
System.out.println();
}

int [][] player1_tiles = new int[7][49];
int [][] player2_tiles = new int[7][49];
int [] tile1 = null;
int [] tile2 = null;

for(int i=0;i<player1_tiles.length;i++){

tile1 = tiles.get(gen.nextInt(tiles.size()));
tile2 = tiles.get(gen.nextInt(tiles.size()));
tiles.remove(tile1);
tiles.remove(tile2);

player1_tiles[i] = tile1;
player2_tiles[i] = tile2;
}
System.out.println("\n=====================================");
System.out.println("\nTile\tPemain 1\tPemain 2");
for(int i=0;i<player1_tiles.length;i++){
System.out.print((i+1) +": \t ");
for(int j=0;j<player1_tiles[i].length;j++){
System.out.print(player1_tiles[i][j] + " ");
}
System.out.print("\t\t");

for(int j=0;j<player2_tiles[i].length;j++){
System.out.print(player2_tiles[i][j] + " ");
}

System.out.println();
}
System.out.println();
if(tile1[0]>tile2[1] && tile1[1]>tile2[1]){
System.out.println("Hasilnya : Pemain 1 Duluan");
}else{
System.out.println("Hasilnya : Pemain 2 Duluan");

}
    }
}


 Jika telah selesai, maka jalankan Class nya :


--------------------------------------------------------------------------------------------

Untuk tambahan , anda dapat mempersingkat Koding dengan mengganti koding utama dengan koding dibawah ini, dan silahkan lanjutkan peyelesaiannya :) :

public class domino{ 
 public static void main(String args[]){ 

  ArrayList<Point> tiles = new ArrayList<Point>(); 
  Random gen = new Random(); 

  for(int i=0;i<7;i++){ 
   for(int j=0;j<7;j++){ 
    tiles.add(new Point(i,j)); 
    System.out.print("("+ i + ", " + j + ") "); 
   } 
   System.out.println(); 
  } 

  Point [] player1_tiles = new Point[7]; 
  Point [] player2_tiles = new Point[7]; 
  Point tile1 = null; 
  Point tile2 = null; 

  for(int i=0;i<player1_tiles.length;i++){ 

   tile1 = tiles.get(gen.nextInt(tiles.size()));  
   tile2 = tiles.get(gen.nextInt(tiles.size())); 
   tiles.remove(tile1); 
   tiles.remove(tile2); 

   player1_tiles[i] = tile1; 
   player2_tiles[i] = tile2; 
  } 








Tidak ada komentar:

Poskan Komentar