/ Published in: Java
nim
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.util.Random; import java.util.Scanner; public class Nim { public static int total; { // Yada yada yada... System.out.println("match looses. First, however, we need to know how many matches we will play with.\n"); // Get answer int num = input.nextInt(); total = (num<25||num>100) ? 25 : num; // And boom goes the dynamite... do { int response = input.nextInt(); if (response < 1 || response > 3) System.out.print("Not valid. You may only pick 1, 2 or 3 matches!\n"); else { total -= response; myTurn(); } while (total > 1); if (total == 1) System.out.println("Oh oh, you have to take the last match!. You loose, muhahaha."); } public static void myTurn() { int take; // Nim algorithm goes here... // ... // No algorithm... take = generator.nextInt(3) + 1; // Subtract taken matches total -= take; // Report back } }