Pages

FunSimplePuzzles


1. Siamese method for solving n-odd Magic Square


/**
 * The Siamese method, or De la Loubère method, is a simple method to construct
 * any size of n-odd magic squares. a magic square is an arrangement of integers
 * in a square grid, where the numbers in each row, and in each column, and the
 * numbers in the forward and backward main diagonals, all add up to the same
 * number.
 * 
 * This implementation of Siamese method generates n by n html table which can
 * easily be integrated in an android program (to be used inside WebView).
 * 
 * @author Ayush Subedi
 */
public class SiameseMagicSquare {

 /**
  * @param args
  */
 public static void main(String[] args) {

  // input most likely from an Android editText
  int dimension = 3;

  // dimension in the form 2n+1
  if (dimension % 2 == 0) {
   throw new Error("Siamese only works with odd dimension square");
  }

  int[][] magicSquare = new int[dimension][dimension];
  // by java default, all entries of magicSquare is initially 0

  // initial filling
  int row = 0;
  int col = dimension / 2;
  magicSquare[row][col] = 1;

  // following the algorithm
  for (int i = 2; i <= dimension * dimension; i++) {
   if (magicSquare[(row - 1 + dimension) % dimension][(col + 1)
     % dimension] == 0) {
    row = (row - 1 + dimension) % dimension;
    col = (col + 1) % dimension;
   } else {
    row = (row + 1) % dimension;
   }
   magicSquare[row][col] = i;
  }

  String body = "<!DOCTYPE html><html><body><table border =\"1\" cellpadding=\"10\">";

  for (int i = 0; i < dimension; i++) {
   // new Row
   body = body + "<tr>";

   for (int j = 0; j < dimension; j++) {
    // new Column and Magic Square entry
    body = body + "<td>" + magicSquare[i][j] + "</td>";
   }
   body = body + "</tr>";
  }
   body = body + "</table></body></html>";
                // The result "body" can be displayed in Android WebView.
 }
}

html output:



<!DOCTYPE html>
<html>
<body>
 <table cellpadding="10" border="1">
  <tr>
   <td>8</td>
   <td>1</td>
   <td>6</td>
  </tr>

  <tr>
   <td>3</td>
   <td>5</td>
   <td>7</td>
  </tr>

  <tr>
   <td>4</td>
   <td>9</td>
   <td>2</td>
  </tr>

 </table>
</body>
</html>

Result

____________________________________________________

***********************************************

Three Card Brag


import java.util.*; 
//For ArrayList, Arrays, List and Random

/**
 * Three card brag is a 16th-century British card game, and the British national 
 * representative of the "bluffing" family of gambling games. Brag is a 
 * direct descendant of the Elizabeth game of Primero and one of the several 
 * ancestors to poker, just varying in betting style and hand rankings. 
 * The game is very popular in Trinidad, India, Bangladesh and Nepal, where it 
 * is known both as "Flush" and "Teen Patti" (literally translated from Hindi as 
 * "three cards"), played with some minor variations.(Wikipedia)
 * 
 * The implementation is a popular variation known as Open Flush or Open Teen Patti.
 * [2 players] 
 * 
 * @author Ayush Subedi
 * 
 */
public class card {
 /**
  * c1, c2, c3: three cards assigned to player 1/computer
  * p1, p2, p3: three cards assigned to player 2/user
  */ 
 String c1, p1, c2, p2, c3, p3;  
 
 /**
  * Default constructor: Randomly generated two set of cards
  */
 public card() {
  List<String> deckOfCard = new ArrayList<String>
  (Arrays.asList(new String[] 
    { "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", "i1", "j1", "k1", "l1", "m1", 
      "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", "i2", "j2", "k2", "l2", "m2", 
      "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3", "i3", "j3", "k3", "l3", "m3", 
      "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4", "i4", "j4", "k4", "l4", "m4" }));

  
  //First randomly generated card to the computer, the next one to user, 
  //the next one to computer and so on.. (for fairness)
  //Also the randomly assigned card is removed from the deck before another
  //random card is drawn.
  int cardIndex;
  c1 = deckOfCard.get(cardIndex = random(deckOfCard.size()));
  deckOfCard.remove(cardIndex);
  p1 = deckOfCard.get(cardIndex = random(deckOfCard.size()));
  deckOfCard.remove(cardIndex);
  c2 = deckOfCard.get(cardIndex = random(deckOfCard.size()));
  deckOfCard.remove(cardIndex);
  p2 = deckOfCard.get(cardIndex = random(deckOfCard.size()));
  deckOfCard.remove(cardIndex);
  c3 = deckOfCard.get(cardIndex = random(deckOfCard.size()));
  deckOfCard.remove(cardIndex);
  p3 = deckOfCard.get(cardIndex = random(deckOfCard.size()));
 }

 // Non-default constructor: mainly for testing purposes
 public card(String c1, String p1, String c2, String p2, String c3, String p3) {
  this.c1 = c1;
  this.p1 = p1;
  this.c2 = c2;
  this.p2 = p2;
  this.c3 = c3;
  this.p3 = p3;
 }

 //getters 
 public String getC1() {
  return c1;
 }

 public String getP1() {
  return p1;
 }

 public String getC2() {
  return c2;
 }

 public String getP2() {
  return p2;
 }

 public String getC3() {
  return c3;
 }

 public String getP3() {
  return p3;
 }

 
 /**
  * @param size: Size of the deckOfCard
  * @return : Random index of a card from the deck of @param size
  */
 public int random(int size) {
  return new Random().nextInt(size);
 }

 @Override
 public String toString() {
  return "Computer: " + c1 + " " + c2 + " " + c3 + "\n" +
      "     You: " + p1 + " " + p2 + " " + p3;
 }

}

Some methods in the Main class:

 public boolean youWin() {
  //if player 1/computer draws a trial and you don't
  if (istrial(card.getC1(), card.getC2(), card.getC3()) && !istrial(card.getP1(), card.getP2(), card.getP3())) {
   return false;
  }
  //if you draw a trial and player 1/computer does not 
  if (!istrial(card.getC1(), card.getC2(), card.getC3()) && istrial(card.getP1(), card.getP2(), card.getP3())) {
   return true;
  }
  //if both draw a trial
  if (istrial(card.getC1(), card.getC2(), card.getC3()) && istrial(card.getP1(), card.getP2(), card.getP3())) {
   return higherCard(card.getC1(), card.getC2(), card.getC3(),card.getP1(), card.getP2(), card.getP3());
  }
  
  // .... ..........  //  
  return true;
 }

Next, check for "Run" & "Color"
Next, check for "Run"
Next, check for "Color"
........... and so on
if nothing is satisfied, check for higher cards.


 
//..................................//
public static boolean istrial(String a, String b, String c) {
  if (a.charAt(0) == b.charAt(0) && a.charAt(0) == c.charAt(0) && b.charAt(0) == c.charAt(0)) {
   return true;
  }
  return false;
 }

 public static boolean isjute(String a, String b, String c) {

  if (a.charAt(0) == b.charAt(0) || a.charAt(0) == c.charAt(0) || b.charAt(0) == c.charAt(0)) {
   return true;
  }
  return false;
 }

 public static boolean iscolor(String a, String b, String c) {
  if (a.charAt(1) == b.charAt(1) && a.charAt(1) == c.charAt(1) && b.charAt(1) == c.charAt(1)) {
   return true;
  }
  return false;
 }
//..................................//

Android layout:



____________________________________________________

***********************************************

Luhn's Algorithm for validity of credit card numbers

Algorithm used ---->
public class LuhnAlgorithm {

	// checks the validity of any 16 digit Credit Card
	public static void main(String[] args) {
		String CardNumber = "4485641126599352";
		if (validity(CardNumber)) {

			if (CardNumber.substring(0, 1).equals("4")) {
				System.out.print("Visa");
			} else if (CardNumber.substring(0, 2).equals("51") || CardNumber.substring(0, 2).equals("52") || CardNumber.substring(0, 2).equals("53") || CardNumber.substring(0, 2).equals("54")) {
				System.out.print("MasterCard");
			}

			else if (CardNumber.substring(0, 4).equals("6011") || CardNumber.substring(0, 3).equals("644") || CardNumber.substring(0, 2).equals("65")) {
				System.out.print("Discover");
			} else {
				System.out.print("Unknown Issuer but valid number");
			}
		} else {
			System.out.print("Invalid Card Number");
		}
	}

	public static boolean lengthCheck(String localCardNumber) {
		return 16 == localCardNumber.length();
	}

	public static boolean isValidLong(String localCardNumber) {
		try {
			Long.parseLong(localCardNumber);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public static boolean validity(String localCardNumber) {
		if (lengthCheck(localCardNumber) && isValidLong(localCardNumber)) {
			int doublesum = 0;
			int sum = 0;
			int n;

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

				if (i % 2 == 0) {
					n = 2 * Integer.parseInt(localCardNumber.substring(i, i + 1));
					sum = 0;
					while (n != 0) {
						sum += n % 10;
						n /= 10;
					}
				} else {
					sum = Integer.parseInt(localCardNumber.substring(i, i + 1));
				}
				doublesum = doublesum + sum;
			}
			return (doublesum % 10 == 0);
		}
		return false;
	}

}

___________________________________________________

***********************************************

To Do List:

1. N-Queen problem using recursion and backtracking
2. Sudoku using recursion and backtracking
3. Find some more 



Ayush Subedi

Coffee Connoisseur