Jump to content

User:HimpThePimp

From Wikipedia, the free encyclopedia

That's right! I'm using my own talk page to pass homework stuff back and forth to myself from school to home. class Nim2 { public static void main(String [ ] args) { System.out.print("Welcome to Nim!\n\n\n");

EasyReader reader = new EasyReader(); int turn = 0; String dummy;

player p1 = new player(); //creates as many human players as the user wants int numofplay =p1.inputNum("How many players?(1-30) ",1,30); int humans = p1.inputNum("How many of these are human?", numofplay-9, numofplay); player pn[] = new player[numofplay]; System.out.print("\n");

//gives these players names and avatars for(int i = 0; i < humans; i++) { System.out.print("What's your name player "+(i+1)+"? "); String tempstrg; tempstrg = reader.readLine(); System.out.print("\nEnter a character to use as your" +" avatar: "); char tempchar = reader.readChar(); pn[i] = new player(tempstrg, tempchar); dummy = reader.readLine(); System.out.print("\n"); }


for(int i = 0; i < numofplay-humans; i++) { pn[i] = new AIplayer(i+1); } //creates a new "columns" object, which the user sets columns j = new columns(p1);

//mov will hold user inputs. After these are digested //pos will remember the position of the ">" int mov; int pos = 0;

//Lets the user play until there are no more tokens while(j.Gameover()==false) { j.displaytok(pos,pn[turn%numofplay]); mov = pn[0].inputNum("", 1, 9); //If the user enters "5" it calls removetok if(mov==5) { int oldMax = j.getmaxtok(); if(j.removetok(pos)==true) { turn++; AIplayer AI1 = new AIplayer(2); System.out.print(AI1.getNimSum(j)); if(oldMax - j.getmaxtok() != 0) pos = j.fudge(pos); } } //moves ">" if(j.goodpos(pos + j.move(mov))==true) pos += j.move(mov); // System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); } turn--; System.out.print("After " + (turn+1) + " turns " + pn[turn%numofplay].getname() + " lost the game!!\n\nWinner"); if(numofplay > 2) System.out.print("s");

System.out.print(": "); for(int i = 0; i < numofplay-1; i++) { turn++; System.out.print(pn[turn%numofplay].getname() + "\n "); if(numofplay > 2) System.out.print(" "); } System.out.print("\n"); }

}


class player { String name; char avatar;

player() {}

player(String usename, char useavatar) { name = usename; avatar = useavatar; }

String getname() {return name;}

char getavatar() {return avatar;}

public static int inputNum(String prompt, int min, int max) { if (min < 0) min = 0; System.out.print(prompt); EasyReader reader = new EasyReader(); String SNum = reader.readLine(); int rValue; //not-a-number test try { Integer wrap = new Integer(SNum); rValue = wrap.intValue(); } catch (NumberFormatException e) {rValue = inputNum("\nNot a number.\n" + prompt, min, max);} //too big too small test if(rValue < min || rValue > max) rValue = inputNum("Must be greater than " + (min-1) + " and less than " + (max+1) + ".\n" + prompt, min, max); return rValue; }

public static int inputNum(String prompt, int min, int max, columns unused) { if (min < 0) min = 0; System.out.print(prompt); EasyReader reader = new EasyReader(); String SNum = reader.readLine(); int rValue; //not-a-number test try { Integer wrap = new Integer(SNum); rValue = wrap.intValue(); } catch (NumberFormatException e) {rValue = inputNum("\nNot a number.\n" + prompt, min, max);} //too big too small test if(rValue < min || rValue > max) rValue = inputNum("Must be greater than " + (min-1) + " and less than " + (max+1) + ".\n" + prompt, min, max); return rValue; } }

class AIplayer extends player { AIplayer(int number) { name = "Computer" + number; String temp = "" + number; avatar = temp.charAt(0); }


static int pow(int base, int exponent) { Double result = new Double(Math.pow(base, exponent)); return result.intValue(); }


int getNimSum(columns reference) { int nimsum = 0; columns board = new columns(reference); for (int exp = 0; pow(2,exp) <= board.getmaxtok(); exp++) { int evnodd = 0; for (int col = 0; col < board.getcol(); col++) { evnodd += board.gettok(col) % pow(2,exp+1); if (board.gettok(col) % pow(2,exp+1) == 1) board.settok(col, board.gettok(col)-pow(2,exp)); } if ((evnodd / pow(2,exp) % 2) == 1) nimsum += pow(2,exp); } return nimsum; }

int choosePoint(columns board) { int nimsum = getNimSum(board); int col = 0; for(int tokens = 0; tokens >= nimsum && col <= board.getcol(); col++) tokens = board.gettok(col); return (board.getmaxtok() - nimsum) * board.getcol() + col; }

// public static int inputNum(String hidden1, int hidden2, int hidden3, columns board) // { // ; // } }

class columns { int mycol; int[] mytok;

//creates a useless dummy class columns(){}

columns(columns image) { mycol = image.getcol(); mytok = image.mytok; }

int gettok(int col) {return mytok[col];}

int getcol() {return mycol;}

void settok(int col, int tok) {mytok[col] = tok;}

//asks the user to set columns(player play) { //Sets "mycol" mycol = play.inputNum("How many columns do you want? ", 1, 20); System.out.print("\n");

//Sets "mytok" int [] toks = new int [mycol] ; for (int i = 0; i < mycol; i++) toks[i] = play.inputNum("Column "+ (i+1) + ": How many tokens do you want? ", 0, 50); mytok = toks; }

//returns the maximum tokens in all columns int getmaxtok() { int max = 0; for (int i = 0; i < mycol; i++) { if (mytok[i] > max) max = mytok[i]; } return max; }

//takes user input returns the change in position int move (int use) { int val=0; if(use==7||use==8||use==9) val=-1*mycol; if(use==1||use==2||use==3) val=mycol; if(use==7||use==4||use==1) val--; if(use==9||use==6||use==3) val++; return val; }

//returns vertical position based on value int posv(int use) {return getmaxtok() - use/mycol;}

//returns horizontal position based on value int posh(int use) {return use%mycol;}

//makes sure > is on the board boolean goodpos(int use) { if(posh(use) >=0 && posh(use) < mycol && posv(use) > 0 && posv(use) <= getmaxtok()) return true; else return false; }

int fudge (int pos) { int fudgeNum = posh(pos) + (getmaxtok()-mytok[posh(pos)]) * mycol; if(mytok[posh(pos)] == getmaxtok()) //example: # ># - return fudgeNum; else //example: # >- - return fudgeNum - mycol; }

//returns "true" when all tokens have been removed boolean Gameover() { for (int i = 0; i < mycol; i++) if(mytok[i] != 0) return false; return true; }

//removes tokens in a single column boolean removetok(int position) { int col = posh(position); int tokens = posv(position)-1; if(tokens < mytok[col]) { mytok[col] = tokens; return true; } else return false; }

//display token set up void displaytok(int point, player guy) { //note that the a 3x3 board will look like /* 0 1 2 ph

3 0 1 2 2 3 4 5 1 6 7 8 tok */ //pv and ph hold the ">"'s position int pv = posv(point); int ph = posh(point);

System.out.println(guy.getname() + "'s turn to move."); for (int i = getmaxtok(); i > 0; i--) { for (int j = 0; j < mycol; j++) { if (pv==i&&ph==j) System.out.print(guy.getavatar()); else System.out.print(" "); if(mytok[j] >= i) System.out.print("# "); else System.out.print ("- "); } System.out.print("\n"); } } }