AP Membuat Game Sudoku

SUDOKU

Kali ini kita akan mencoba membuat game sudoku.
Codingan :


import java.util.Scanner;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author SALSABILA
*/
public class sudoku {

   static Scanner scan = new Scanner(System.in);
   static int p[][];
   static String g[][];
   static char[] huruf;
   static int a = 5;
   static int b = 14;
   static int c = 26;
   static int life = 5;

   static void random(int n) {
       p = new int[n][n];
       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n; j++) {
               int loopcount = 0;
               int x;
               int temp_i = i;
               int temp_j = j;
               boolean check = true;
               do {
                   x = (int) (Math.random() * n) + 1;
                   check = true;
                   temp_i = i - 1;
                   temp_j = j - 1;
                   //check atas
                   while (temp_i >= 0) {
                       if (p[temp_i][j] == x) {
                           check = false;
                           break;
                       }
                       temp_i--;
                       

                   }
                   while (temp_j >= 0) {
                       if (p[i][temp_j] == x) {
                           check = false;
                           break;
                       }
                       temp_j--;
                   }
                   loopcount++;
                   if (loopcount > 60) {
                       break;
                   }
               } while (!check);
               if (loopcount > 20) {
                   j = 0;
               } else {
                   p[i][j] = x;
               }
           }

       }
       h(n);
   }

   static void h(int n) {
       g = new String[n][n];
       int x;
       huruf = new char[n];
       char huruf = 'A';
       int tahu = 0;
       if (n == 5) {
           for (int i = 0; i < n; i++) {
               for (int j = 0; j < n; j++) {
                   x = (int) ((Math.random() * 100) + 1);
                   if (x < 35 && tahu < 5) {
                       g[i][j] = String.valueOf(huruf);
                       huruf++;
                       tahu++;
                   } else {
                       g[i][j] = String.valueOf(p[i][j]);
                   }
               }
           }
       } else if (n == 9) {
           for (int i = 0; i < n; i++) {
               for (int j = 0; j < n; j++) {
                   x = (int) ((Math.random() * 100) + 1);
                   if (x < 32 && tahu < 14) {
                       g[i][j] = String.valueOf(huruf);
                       huruf++;
                       tahu++;
                   } else {
                       g[i][j] = String.valueOf(p[i][j]);
                   }
               }
           }
       } else if (n == 16) {
           for (int i = 0; i < n; i++) {
               for (int j = 0; j < n; j++) {
                   x = (int) ((Math.random() * 100) + 1);
                   if (x < 25 && tahu < 26) {
                       g[i][j] = String.valueOf(huruf);
                       huruf++;
                       tahu++;
                   } else {
                       g[i][j] = String.valueOf(p[i][j]);
                   }
               }
           }

       }

       tampil(n);
   }

   static void tampil(int n) {

       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n; j++) {
               System.out.print(g[i][j] + "\t");
           }
           System.out.println("");
       }
       life(n);
   }

   static void life(int n) {
       System.out.print("Lives ");
       for (int j = 0; j < life; j++) {
           System.out.print(" O");
       }
       System.out.println("");
       System.out.println("");
       soal(n);
   }

   static void soal(int n) {
       String letter;
       int jawab;
       System.out.print("Letter to guess: ");
       letter = scan.next();
       System.out.print(letter + ": ");
       jawab = scan.nextInt();
       if (n == 5) {
           for (int i = 0; i < n; i++) {
               for (int j = 0; j < n; j++) {
                   if (g[i][j].equalsIgnoreCase(letter)) {
                       if (p[i][j] == jawab) {
                           System.out.println(
                                   "Nice");
                           g[i][j] = String.valueOf(jawab);
                           a--;
                           if (a > 0) {
                               tampil(n);
                           } else {
                               end();
                               break;
                           }

                       } else {
                           System.out.println("Salah");
                           life--;
                           if (life == 0) {
                               System.out.println(
                                       "Lives");
                               System.out.println("=== GAME OVER ===");
                               tanya();
                           } else {
                               tampil(n);
                           }
                       }
                   }
               }
           }
       } else if (n == 9) {
           for (int i = 0; i < n; i++) {
               for (int j = 0; j < n; j++) {
                   if (g[i][j].equalsIgnoreCase(letter)) {
                       if (p[i][j] == jawab) {
                           System.out.println(
                                   "Nice");
                           g[i][j] = String.valueOf(jawab);
                           b--;
                           if (b > 0) {
                               tampil(n);
                           } else {
                               end();
                               break;
                           }

                       } else {
                           System.out.println("Salah");
                           life--;
                           if (life == 0) {
                               System.out.println(
                                       "Lives ");
                               System.out.println("=== GAME OVER ===");
                               tanya();
                           } else {
                               tampil(n);
                           }
                       }
                   }
               }
           }
       } else if (n == 16) {
           for (int i = 0; i < n; i++) {
               for (int j = 0; j < n; j++) {
                   if (g[i][j].equalsIgnoreCase(letter)) {
                       if (p[i][j] == jawab) {
                           System.out.println(
                                   "Nice");
                           g[i][j] = String.valueOf(jawab);
                           c--;
                           if (c > 0) {
                               tampil(n);
                           } else {
                               end();
                               break;
                           }

                       } else {
                           System.out.println("Tidak");
                           life--;
                           if (life == 0) {
                               System.out.println(
                                       "Lives");
                               System.out.println("=== GAME OVER ===");
                               tanya();
                           } else {
                               tampil(n);
                           }
                       }
                   }
               }
           }
       }
   }

   static void end() {
       if (a == 0 || b == 0 || c == 0) {
           System.out.println("========= SELAMAT =========");
           System.out.println("======== Kamu Menang ========");
       }
       tanya();
   }

   static void tanya() {
       System.out.println("Main Lagi? (y/n)? ");
       String j = scan.next();
       if (j.equalsIgnoreCase("Y")) {
           life = 5;
           level();
       } else if (j.equalsIgnoreCase("N")) {
           exit();
       } else {
           System.out.println("Outputh");
           tanya();
       }
   }

   static void level() {
       System.out.println(
               "LEVEL");
       System.out.println("    1. EASY");
       System.out.println("    2. PRO");
       System.out.println("    3. SUPER");
       System.out.print("PLAYL : ");
       int level = scan.nextInt();
       if (level == 1) {
           System.out.println("");
           System.out.println(" EASY ");
           random(5);
       } else if (level == 2) {
           System.out.println("");
           System.out.println(" PRO ");
           random(9);
       } else if (level == 3) {
           System.out.println("");
           System.out.println(" SUPER ");
           random(16);
       }
   }

   static void exit() {
       System.out.println("");
       System.out.println("Thank you for Playing~");
   }

   public static void main(String[] args) {
       System.out.println(" WELCOME TO SUDOKU");
       System.out.println("====================");
       level();
   }
}

Sekian postingan dari saya , Semoga bermanfaat bagi anda.
Terima Kasih

Comments