Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Nim Game and Goldbach's Conjecture
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HeavenAgain




PostPosted: Mon Mar 05, 2007 10:23 pm   Post subject: Nim Game and Goldbach's Conjecture

well, i programed 2 simple program today, one is the Goldbach's Conjecture and another is the Nim game; where there are 4 piles, each pile contains 4 - 8 sticks, and you take an amout of sticks from one pile only, taking turns, the one who takes the last stick loses! (note, this game is for two players only, i havent build a computer player yet)

Goldbach
Java:

import java.io.*;
class GCassignment
{
  public static int nextNumber()throws Exception
  {
    BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
    int number;
    do
    {
      System.out.println("Please enter an even integer greater than 2...Enter 0 to stop.");
      number = Integer.parseInt(in.readLine());
      if (number == 0)
        System.out.println("Exiting program");
      else if (number < 2)
        System.out.println("Must be greater than 2! Please enter again.");
      else if (number % 2 != 0)
        System.out.println("Not an even number! Please enter again.");
      else
        ;
    }
    while (number < 0 || number % 2 == 1 || number == 2);
    return number;
  }
  public static boolean IsPrime(int a)
  {
    boolean check = true;
    for (int i = 2 ; i != (a/2)+1 ; i++)
    {
      if (a%i == 0)
        check = false;
    }
    return check;
  }
  public static void testGoldbach(int number)
  {
    int firstnumber,secondnumber, counter = 0;
    firstnumber=2;
    boolean done = false;
    System.out.println("Generated prime number pairs:");
    System.out.println("\n     First Number     Second Number");
    while (!done)
    {
      if (firstnumber <= (number/2))
      {
        if (IsPrime(number-firstnumber))
        {
          secondnumber=number-firstnumber;
          counter++;
          System.out.printf("\n %11d %17d", firstnumber, secondnumber);
          firstnumber = nextPrime(firstnumber);
        }
        else;
        firstnumber = nextPrime(firstnumber);
      }
      else
        done = true;
    }
   
    if (counter >=1 )
    {
      System.out.println("\n\nThere are " + counter + " prime number combinations.");
      System.out.println("The Goldbach conjecture has been proven to be true.");
    }
    else
    {
      System.out.println("No prime number combinations generated.");
      System.out.println("The Goldbach conjecture has been proven to be false.");
    }
  }
  public static int nextPrime(int b)
  {
    int value = b;
    if (b == 2)
      value = 3;
    else
    {
      do
      {
        value += 2;
      } while (!IsPrime(value));
    }
    return value;
  }
  public static void main (String[] args)throws Exception
  {
    int value = nextNumber();
    while (value != 0)
    {
      testGoldbach(value);
      System.out.println();
      value = nextNumber();
    }
  }
}


Nim
Java:

import java.io.*;
import javax.swing.*;
class Nim
{
  public static void main (String [] args)throws Exception
  {
    BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
    String player1 = JOptionPane.showInputDialog(null, "Player One Name", "Player One", JOptionPane.PLAIN_MESSAGE);
    String player2 = JOptionPane.showInputDialog(null, "Player Two Name", "Player Two", JOptionPane.PLAIN_MESSAGE);
    int[] sticks= {Sticks(),Sticks(),Sticks(),Sticks()};
    int pick;
    int counter=0,winner=0;
    int checker;
    while (check(sticks[0],sticks[1],sticks[2],sticks[3]))
    {
      counter++;
      System.out.println("\n"+sticks[0]+" "+sticks[1]+" "+sticks[2]+" "+sticks[3]);
      if (counter%2==0)
      {
        System.out.println(player1+" its your turn");
        winner=1;
      }
      else
      {
        System.out.println(player2+" its your turn");
        winner=0;
      }
      System.out.println("which pile to take?");
      pick = Integer.parseInt(in.readLine());
      System.out.println(pick);
      if (pick<=4 && pick >0)
      {
        if (pick==1)
        {
          if(sticks[0]!=0) 
          {
            checker=sticks[0];
            sticks[0]-=Take(sticks[0],sticks[1],sticks[2],sticks[3]);         
            if (checker==sticks[0])
              counter--;
          }
          else
            System.out.println("there is no sticks to take\n");
        }
        if (pick==2)
        {
          if (sticks[1]!=0)
          {
            checker=sticks[1];
            sticks[1]-=Take(sticks[1],sticks[0],sticks[2],sticks[3]);
            if(checker==sticks[1])
              counter--;
          }
          else
{
            System.out.println("there is no sticks to take\n");
counter--;
} 
      }
        if (pick==3)
        {
          if (sticks[2]!=0)
          {
            checker=sticks[2];
            sticks[2]-=Take(sticks[2],sticks[0],sticks[1],sticks[3]);
            if(checker==sticks[2])
              counter--;
          }
          else
            System.out.println("there is no sticks to take\n");
        }
        if (pick==4)
        {
          if (sticks[3]!=0)
          {
            checker=sticks[3];
            sticks[3]-=Take(sticks[3],sticks[0],sticks[1],sticks[2]);
            if (checker==sticks[3])
              counter--;
          }
          else
            System.out.println("there is no sticks to take\n");
        }
      }
      else
        System.out.println("invalid pile\n");
    }
    System.out.println("\n"+sticks[0]+" "+sticks[1]+" "+sticks[2]+" "+sticks[3]);
    if (winner==1)
      System.out.println(player1+" Very nice! You have won the game in "+counter+" moves!");
    else
      System.out.println(player2+" Very nice! You have won the game in "+counter+" moves!");
  }
 
  public static int Sticks()
  {
    return (int)(4+(Math.random()*5));// int num = (int)start+(Math.random()*(end - start + 1));
  }
 
  public static int Take(int sticks, int other1,int other2, int other3) throws Exception
  {
    BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
    System.out.println("How much do you wish to take? made a mistake? enter 0 to go back");
    if (other1==0 && other2==0 && other3 == 0)
      System.out.println("Note: you cannot take more than: "+sticks);
    int take = Integer.parseInt(in.readLine());
    System.out.println(take);
    boolean check=false;
    do
    {
      check=false;
      if (take==sticks && other1==0 && other2==0 && other3 == 0)
      {
        System.out.println("sorry you cannot take more than "+sticks+" Please try again, made a mistake? enter 0 to go back");
        take = Integer.parseInt(in.readLine());
        System.out.println(take);
        check=true;
      }
      else if(take>sticks || take<0)
      {
        System.out.println("sorry you cannot take more than "+sticks+" Please try again, made a mistake? enter 0 to go back");
        take = Integer.parseInt(in.readLine());
        System.out.println(take);
        check=true;
      }
    }while (check);
    return take;
  }
 
  private static boolean check(int a, int b, int c, int d)
  {
    boolean check = true;
    if (a==1&&b==0&&c==0&&d==0)
      check= false;
    if(a==0&&b==1&&c==0&&d==0)
      check = false;
    if (a==0&&b==0&&c==1&&d==0)
      check = false;
    if (a==0&&b==0&&c==0&&d==1)
      check = false;
    return check;
  }
}
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: