Thursday, May 1, 2014

21 Match Sticks Game

21 Match Sticks is a small game. That can be implemented easily in any programming language.

In this game, user/computer needs to pick 1 to 4 sticks and whoever picks the last stick will lose the game.

import java.util.Scanner;

public class MatchSticks21 {

      public static void main(String[] args) {
            int matchstick = 21;
            int user = 0;
            int computer = 0;
            Scanner scanner = new Scanner(System.in);

            while (matchstick >= 1) {

                  if (matchstick == 1) {
                        System.out.println("Match stick status: " + matchstick);
                        System.out.println("\nYou loose!!!!!!:(:(");
                        break;
                  }                            
                  System.out.println("Match stick status: " + matchstick);
                  System.out.println("\nEnter the choice (1,2,3,4)):");
                  user = scanner.nextInt();

                  System.out.println("\nYou picked: " + user);
                  if (user >= 5 || user <= 0) {
                        System.out.println("\nInvalid value");
                        continue;
                  }
                  computer = 5 - user;

                  System.out.println("\nComputer picked " + computer);
                  matchstick = matchstick - computer - user;
            }
      }
}


Solving Sudoku in Simpler Way

Here, I have presented simple logic to solve sudoku with simple methods.

public class MySudoku {
      public static void main(String[] args) {
            new MySudoku(new int[9][9]).display();
      }

      private final int[][] sudoku;

      public MySudoku(int[][] sudoku) {
            this.sudoku = sudoku;
            solve(0, 0);
      }

      public boolean checkColumn(int row, int value) {
            for (int column = 0; column < 9; column++) {
                  if (sudoku[row][column] == value) {
                        return false;
                  }
            }
            return true;
      }

      public boolean checkRow(int column, int value) {
            for (int row = 0; row < 9; row++) {
                  if (sudoku[row][column] == value) {
                        return false;
                  }
            }
            return true;
      }

      public boolean checkBox(int row, int column, int value) {
            int boxRow = (row / 3) * 3;
            int boxColumn = (column / 3) * 3;
            for (int aRow = 0; aRow < 3; aRow++) {
                  for (int aColumn = 0; aColumn < 3; aColumn++) {
                        if (sudoku[boxRow + aRow][boxColumn + aColumn] == value) {
                              return false;
                        }
                  }
            }
            return true;
      }

      public boolean solve(int row, int column) {
            if (row == 9) {
                  row = 0;
                  column = column + 1;
                  if (column == 9) {
                        return true;
                  }
            }

            if (sudoku[row][column] != 0) {
                  return solve(row + 1, column);
            }

            for (int value = 1; value <= 9; value++) {
                  if (checkBox(row, column, value)
                              && checkColumn(row, value)
                              && checkRow(column, value)) {
                        sudoku[row][column] = value;
                        if (solve(row + 1, column)) {
                              return true;
                        }
                  }
            }
            sudoku[row][column] = 0;
            return false;
      }

      public void display() {
            for (int row = 0; row < 9; row++) {
                  System.out.println();
                  for (int column = 0; column < 9; column++) {
                        System.out.print("\t" + String.valueOf(sudoku[row][column]));
                  }
                  System.out.println();
            }
      }

}