Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Scoreboard

807606Mar 21 2007 — edited Mar 21 2007
I've been asked to write code for a game of reversi in Java, some of which I have been given, which I have worked at for ages and eventually got some sort of reversi game working. The thing is I'm also trying to add a scoreboard to the game as well, I have some code written for it although don't exactly know where to put it in.
Could someone have a look?
If you have any ideas on how to improve the game, please let me know.

import java.io.*;
import java.util.*;


class TextOthello
{


public static void main(String args[])
{

int boardSize = 10;
OthelloBoard board;
Counter currentPlayer;
int x, y ;
Scanner in;


in = new Scanner(System.in);
board = new OthelloBoard(boardSize);
currentPlayer = Counter.BLACK;
System.out.println(board);

// loop until game over
while(!board.finished())
{
System.out.println("Player: "+ currentPlayer);
if(board.canMove(currentPlayer))
{
do
{
System.out.print("Please enter x coordinate of move: ");
x = in.nextInt();
System.out.print("Please enter y coordinate of move: ");
y = in.nextInt();
if(!board.validMove(currentPlayer, x, y))
System.out.println("Invalid move - try again");
}
while(!board.validMove(currentPlayer, x, y));

board.makeMove(currentPlayer, x, y);
System.out.println(board);
System.out.println("------------------------------------------------");
}
else
{
System.out.println("You cannot move - you skip your turn");
} // end if

currentPlayer = OthelloBoard.flip(currentPlayer);

} // end while
}// end main
}// end class



import java.awt.*;
import java.util.*;
import java.io.*;
/*
* full contact info:
* Sieuwert van Otterloo
* Rijnlaan 33bis
* 3522BB Utrecht
* homepage www.bluering.nl or go.to/sieuwert
* phone +31615524227
* coauthor Ernst van Rheenen
*/





public class reversi extends java.applet.Applet{

Label slabel=new Label("please do a move");
board b=new board();
boardview bview=new boardview(b,this);
Button newgame=new Button("new game"),
undo=new Button("undo move");


public reversi(){}


public void init(){
setBackground(Color.white);//you can set the background color of the applet in this line
//resize(bview.SX+30,bview.SY+100); //this line not needed in applets, they cannot resize
Panel buttonpanel=new Panel();//panel to keep buttons together
buttonpanel.setLayout(new GridLayout(1,3));//add three buttons beside each other
buttonpanel.add(newgame);
buttonpanel.add(undo);
Panel superpanel=new Panel();
superpanel.setLayout(new GridLayout(3,1));
superpanel.add(slabel);
superpanel.add(buttonpanel);
setLayout(new BorderLayout());
add("North",bview);
add("South",superpanel);
}


public void start()
{bview.start();}

public void stop()
{bview.stop();}

public boolean action(Event ev, Object O){
if(bview.wait)return true;
if(ev.target==newgame)
new newgamewindow(this);
else if(ev.target==undo)
undo();
return true;
}

public void newgame(){
b.clear();//b.clear() restores the opening position on the board.
bview.repaint();//this updates the screen
}


public void undo(){
if(b.moves!=0)
bview.undomove();
}


public void message(String s){
slabel.setText(s);//set a message in the status label.
}


public static void main(String[] ps)
{Frame f=new Frame("reversi");
reversi r=new reversi();
f.resize(500,500);
f.add("Center",r);
r.init();
r.start();
f.show();
}

}



class newgamewindow extends Frame{
reversi r;
Choice[] cm={new Choice(),new Choice()};
Button start=new Button("start");
Button cancel=new Button("cancel");



public newgamewindow(reversi ir){
super("new game");
r=ir;
r.bview.wait=true;
setLayout(new GridLayout(2,3));
cm[0].add("human");
cm[0].add("computer");
cm[1].add("computer");
cm[1].add("human");
add(cm[0]);
add(new Label(" versus "));
add(cm[1]);
add(start);
add(cancel);
resize(250,100);
setLocation(100,100);
show();
}

public boolean action(Event ev, Object o){
if(ev.target==start){
r.newgame();
r.bview.setplayers(
cm[0].getSelectedIndex()==1,
cm[1].getSelectedIndex()==0);
r.bview.wait=false;
dispose();
}
else if(ev.target==cancel){
r.bview.wait=false;
dispose();
}
return true;
}
}


class boardview extends Canvas
implements Runnable
{
board b;//the board that is viewed
int fieldsize=32;//size in pixels of a field.
int SX,SY;//the total size in pixels of this canvas
reversi top;//the applet that uses this boardview
boolean wait=false;//if wait==true the mouse will be ignored
player[] players={null,null};
boolean[] computer={false,true};
//wether the computer plays that player.
int[] oldboard; //a copy of the A table of the board.

String statusbartext="";//the text under the applet

Color[] r2g={Color.red,
new Color(213,43,0),
new Color(171,85,0),
new Color(128,128,0),
new Color(85,171,0),
new Color(43,213,0),
Color.green};//the colors for the morph
int framenumber;//how far we are in morphing
int frames=r2g.length;//the total number of frames
Graphics g;
Frame superframe;//only in non-applet mode


public boardview(board ib,reversi ir){
top=ir;
b=ib;
myresize(b.X,b.Y);
players[0]=new player(b,0,this);
players[1]=new player(b,1,this);
statusbartext=b.statusmessage();
}

public boardview(board ib){
b=ib;
wait=true;
myresize(b.X,b.Y);
display();
}

public void display(){

superframe=new Frame("reversi");
superframe.resize(SX+20,SY+20);
superframe.add("Center",this);
superframe.show();
}


public boolean mouseDown(Event evt,int x,int y){
if(x>=SX||y>=SY||wait)
return true;
clicked(b.co(x/fieldsize,y/fieldsize));
return true;
}


public void domove(int m){
highlightoff();//stops the morph
b.domove(m);//do the move
statusbartext=b.statusmessage();//set the message
highlighton();//starts the morph
}

public void undomove(){
highlightoff();
b.undomove();
statusbartext=b.statusmessage();
highlighton();
}



void clicked(int c){//called if user clicked field c
if(!computer[b.getplayer()]){
if(b.posmoves==0)
domove(-1);
else if(b.canmove(c))
domove(c);
}
if(computer[b.getplayer()])
computermove();
}

void computermove(){
message("computer is thinking...");
wait=true;
players[b.getplayer()].ask("Please do your move.");

}


public void setplayers(boolean a,boolean b){
computer[0]=a;
computer[1]=b;
}

void myresize(int x,int y){
SX=x*fieldsize;
SY=y*fieldsize;
resize(SX,SY+20);//the 20 is room for the message under the screen.
}

boolean painting=false;

public void paint(Graphics newg){
synchronized(this){
if(painting) return;
painting=true;
}
g=newg;
for(int j=0;j<b.Y;j++)
for(int i=0;i<b.X;i++)
paintfield(i,j);
paintstatusbar();
painting=false;
}

void paintstatusbar(){
g.setColor(Color.black);
g.drawString(statusbartext,10,SY+10);
}


int fading(int i,int j){
if(oldboard==null)
return 0;
g=getGraphics();
int x=b.co(i,j);
if(oldboard[x]==b.P1&&b.get(x)==b.P2)
return 1;
if(oldboard[x]==b.P2&&b.get(x)==b.P1)
return 2;
return 0;
}/*0 means not, 1 r->g, 2 g->r*/

Color back[]={new Color(128,128,128),new Color(100,100,100)};
Color c1=new Color(255,0,0);
Color c2=new Color(0,255,0);

void paintfield(int i,int j){
paintback(i,j);
paintnormalstone(i,j);
}


void fade(){
if(framenumber==-1)
return;
if(framenumber>=frames){
repaint();
framenumber=-1;
return;
}
for(int j=0;j<b.Y;j++)
for(int i=0;i<b.X;i++)
switch(fading(i,j)){
case 0:break;
case 1:
paintback(i,j);
paintstone(i,j,r2g[framenumber]);
break;
case 2:
paintback(i,j);
paintstone(i,j,r2g[frames-1-framenumber]);//r2g[frames-1-framenumber]
break;
}
framenumber++;
}

void paintback(int i,int j){
int si=i*fieldsize,sj=j*fieldsize;
g.setColor(back[(i+j)%2]);
g.fillRect(si,sj,fieldsize,fieldsize);
}

void paintstone(int i,int j,Color c){
int si=i*fieldsize,sj=j*fieldsize;
g.setColor(c);
g.fillRect(si+2,sj+2,fieldsize-4,fieldsize-4);
}

void paintnormalstone(int i,int j){
int v;
boolean canmove;
int flips;
synchronized(this){
v=b.get(i,j);
canmove=b.canmove(b.co(i,j));
flips=b.getflips(b.co(i,j));
}
if(v==b.P1)
paintstone(i,j,c1);
else if(v==b.P2)
paintstone(i,j,c2);
else if(canmove){
int si=i*fieldsize,sj=j*fieldsize;
g.setColor(Color.black);
g.drawString(""+flips,si+fieldsize/2,sj+fieldsize/2);
}
}

void message(String s)
{top.message(s);}

public void answer(String s,int m){
oldboard=(int[])b.A.clone();
domove(m);
wait=false;
message("");
}


void highlightoff(){
oldboard=(int[])b.A.clone();
repaint();
}

void highlighton(){
repaint();
framenumber=0;
}

boolean keeponrunning;



public void start()
{keeponrunning=true;
new Thread(this).start();
}

public void stop()
{keeponrunning=false;}


public void run(){
while(keeponrunning){
try{Thread.sleep(150);}catch(Exception e){}
fade();
}
}

}


class board{

public int[] A;
static int[] all;//all indices of visible fields.
static int X=8,Y=8;/*the size of the visible board. You can play on larger
boards by changing these values. Note that the computer AI will be confused if
you do so. Try to keep X==Y (or modify the kinds code) and recompile all classes
after you did, and retrain the player.*/
static int N=X*Y;//the number of visible fields.
static int BASE=X+2;/*the number used in converting 1dimensional and
twodimensional coordinates*/

//the values A can have.
public static int OUT=88,P1=0,P2=1,FREE=3;


//the moves already done.
int[] move=new int[128];
//the number of stones flipped in that move.
int[] undoflips=new int[128];
//the positions of the stones flipped in that move.
int[][] undoflip=new int[128][24];

int moves; //number of moves done.
int[] can;//the number of flips if you move here,
int posmoves;//the number of moves possible. if it is zero the current player must pass.
int thisp;//the player that is currently moving. valid values are P1 and P2.
int[] posmove=new int[N];/*the moves that are possible.*/
int[] points={0,0};//the points for each player.
int free;//the number of free fields.
int status; /*values:*/final static int RUNNING=1,FINISHED=2;


static int[] kind;//the 'kind' of each field
static int kinds;//the total number of different kinds.
static int evalvectorlength;//the length of an evaluation vector.

static{/*preparations for this class*/
all=new int[N];
int cur=0;
for(int j=0;j<Y;j++)
for(int i=0;i<X;i++)
all[cur++]=co(i,j);
kind=new int[(X+2)*(Y+2)];
int k=0;
for(int i=0;i<X/2;i++)
for(int j=0;j<=i;j++){
kind[co(i,j)]=k;
kind[co(X-1-i,j)]=k;
kind[co(i,Y-1-j)]=k;
kind[co(X-1-i,Y-1-j)]=k;
kind[co(j,i)]=k;
kind[co(j,X-1-i)]=k;
kind[co(X-1-j,i)]=k;
kind[co(X-1-j,Y-1-i)]=k;
k++;
}
kinds=k;
evalvectorlength=kinds+3;

}



static int co(int i,int j) {return (i+1)+((j+1)*BASE);}
//convert from 2dimensional field coordinates to 1dimensional field number.
static int p1(int r) {return r%BASE-1;}//get the x coordinate of a field number
static int p2(int r) {return r/BASE-1;}//get the y coordinate.
static int opponent(int p) {return 1-p;}//returns P1 if given P2 and vice versa.

public board copy(){
board r=new board();
r.moves=moves;
r.posmoves=posmoves;
r.thisp=thisp;
r.A=samearray(A);
r.points=samearray(points);
r.move=samearray(move);
r.posmove=samearray(posmove);
r.can=samearray(can);
return r;
}

int[] samearray(int[] a)
{return (int[])a.clone();}


public board(){
A=new int[(X+2)*(Y+2)];
can=new int[(X+2)*(Y+2)];
for(int i=0;i<A.length;i++)
A=OUT;
clear();
}



public int getplayer() {return moves%2;}

public void set(int i,int value) {A[i]=value;}

public int get(int i) {return A[i];}

public int get(int i,int j) {return A[co(i,j)];}


public void clear(){
moves=0;
for(int i=0;i<N;i++)
A[all[i]]=FREE;
if(2*Math.random()>1){
set(co(3,3),P1);
set(co(3,4),P1);
set(co(4,3),P2);
set(co(4,4),P2);
}else{
set(co(3,3),P2);
set(co(3,4),P1);
set(co(4,3),P1);
set(co(4,4),P2);
}
setcan();
status=RUNNING;
}


void setcan(){
points[P1]=points[P2]=posmoves=0;
free=0;
thisp=getplayer();
for(int i=0;i<N;i++)
if(A[all[i]]==P1)
{points[P1]++;can[all[i]]=0;}
else if(A[all[i]]==P2)
{points[P2]++;can[all[i]]=0;}
else
{
free++;
can[all[i]]=flips(all[i]);
if(can[all[i]]>0)
{
posmove[posmoves++]=all[i];
}
}
if(finished())
status=FINISHED;
else
status=RUNNING;
}


public void domove(int c){

undoflips[moves]=0;
if(status==FINISHED)
return;
if(c!=-1)
{int otherp=opponent(thisp);
int i,dir,d;
A[c]=thisp;
for(i=0;i<8;i++){
dir=around[i];
if(count(otherp,c,dir)>0)
for(d=dir+c;A[d]==otherp;d+=dir){
A[d]=thisp;
undoflip[moves][undoflips[moves]++]=d;
}
}
}
move[moves++]=c;
setcan();
}

//all directions you can flips stones in. Given as the difference in
//1dimensional field numbers.
int[] around={1,-1,BASE,-BASE,BASE+1,BASE-1,-BASE+1,-BASE-1};


int flips(int c){
if(A[c]!=FREE) return 0;
int ret=0;
int otherp=opponent(thisp);
for(int i=0;i<8;i++)
ret+=count(otherp,c,around[i]);
return ret;
}

int count(int otherp,int c,int dir){
int ret=0;
if(A[dir+c]!=otherp)
return 0;
for(c=dir+c;A[c]==otherp;c+=dir)
ret++;
if(A[c]==thisp)
return ret;
return 0;
}


public boolean canmove(int c)
{return can[c]>0;}

public boolean finished()
{return free==0||(posmoves==0&&move[moves-1]==-1);}

public boolean P1wins()
{return points[0]>points[1];}

public boolean P2wins()
{return points[0]<points[1];}

public int getflips(int c){return can[c];}

public int getcoverage()
{return points[0]+points[1];}


int getbalance(){
return points[thisp]-points[1-thisp];
}

int getflipsum(){
int f=0;
for(int i=0;i<N;i++)
f+=can[all[i]];
return f;
}

int getmovecount()
{return posmoves;}



public int[] getevalvector(int[] out){
if(out==null)
out=new int[evalvectorlength];
for(int i=0;i<kinds;i++)
out[i]=0;
for(int i=0;i<N;i++){
int f=A[all[i]];
if(f==thisp)
out[kind[all[i]]]++;
else if(f==1-thisp)
out[kind[all[i]]]--;
}
out[kinds]=getbalance();
out[kinds+1]=getmovecount();
out[kinds+2]=getflipsum();
return out;
}


public void undomove(){
moves--;
int opp=opponent(getplayer());
if(move[moves]!=-1)
A[move[moves]]=FREE;
for(undoflips[moves]--;undoflips[moves]>=0;undoflips[moves]--)
A[undoflip[moves][undoflips[moves]]]=opp;
setcan();
}


public String statusmessage(){
if(finished()){
if(P1wins())
return "RED wins with "+points[P1]+" against "+points[P2];
if(P2wins())
return "GREEN wins with "+points[P2]+" against "+points[P1];
return "The game ended in DRAW";
}
if(thisp==P1)
return "RED to move:"+points[P1]+" (green: "+points[P2]+")";
return "GREEN to move:"+points[P2]+" (red: "+points[P1]+")";
}

}


class player implements Runnable{
/*general variables*/
board realb,b;
int side;
int maxlevel;




public String W="8 0 -7 2 -8 2 0 -2 1 -6 2 3 1 -2 1 1 -2 0 -1 0 1 -2 -1 4 3 5 53 best";
int[] weight=new weightvector(W).a;
int offset;
/*if you used genetictrain to calculate a new weightvector,
change the line String W to hold a line of the new output file.
typically, pick the first or last line. */


/*for use in a parallel thread (applet mode)*/
boardview bv;
long time;



public void setstrength(int l){
if(l>6) l=6;
if(l<1) l=1;
maxlevel=l;
}


public void setweight(int[] w)
{weight=w;}


public player(board ib,int iside)
{this(ib,iside,null);}



public player(board ib,int iside,boardview ibv){
realb=ib;
side=iside;
bv=ibv;
setstrength(4);
}


public void ask(String s){
new Thread(this).start();
}

/*for timing*/
void timestart()
{time=System.currentTimeMillis();}
int gettime()
{return (int)(System.currentTimeMillis()-time);}


public void run(){
timestart();
int move=bestmove();
while(gettime()<4000)
{try{Thread.sleep(400);}catch(Exception e){}}
bv.answer("Well, this is my move:",move);
}


public int bestmove(){
b=realb.copy();
if(b.posmoves==0)
return -1;
setoffset();
int s,best=-1,alpha=-100000;
int k=b.posmoves;
for(int i=0;i<k;i++)
{ b.domove(b.posmove[i]);
s=-prognosis(-100000,-alpha,1);
b.undomove();
if(s>alpha)
{alpha=s;
best=i;
}
}
return b.posmove[best];
}

/*returns how good the current situation looks.*/
int prognosis(int alpha, int beta, int level)
{
if(level>=maxlevel||b.posmoves==0)
return simplescore();
int s;
/*try all moves and return the estimate
we get when doing the best move*/
for(int i=0;i<b.posmoves;i++)
{ b.domove(b.posmove[i]);
s=-prognosis(-beta,-alpha,level+1);
b.undomove();
if(s>beta)
return s;
if(s>alpha)
alpha=s;
}
return alpha;
}


void setoffset(){
/*determines which part of the eval vector,the first or the last, to use.*/
if(b.getcoverage()<weight[weight.length-1])
offset=0;
else
offset=out.length;
}

int[] out;/*this vector can be used again and
again to get an evalvector in. this saves memory. */

int simplescore(){
int score=0;
out=b.getevalvector(out);
for(int i=0;i<out.length;i++)
score+=weight[offset+i]*out[i];
return score;
}
}




class genetictrain
{
int popsize;
weightvector[] pop;
board b=new board();
boardview bv;
player[] P={new player(b,b.P1),new player(b,b.P2)};
boolean onscreen=true;


public genetictrain(int p){
setpopsize(p);
for(int i=0;i<popsize;i++)
pop[i]=new weightvector();
for(int i=0;i<2;i++)
P[i].setstrength(2);
if(onscreen)
bv=new boardview(b);
}


void setpopsize(int n){
popsize=4*(n/4);
pop=new weightvector[popsize];
}


public void read(String filename){
/*read a file back in*/
file f=new file();
f.readopen(filename);
setpopsize(f.num(f.read()));
for(int i=0;i<popsize;i++)
pop[i]=new weightvector(f.read());
f.readclose();
}

public void write(String filename,String comment){
file f=new file();
f.writeopen(filename);
f.write(""+popsize);
for(int i=0;i<popsize;i++)
f.write(""+pop[i]);
f.write(comment+" "+new Date());
f.writeclose();
}


public void run(int rounds){
try{
for(int i=rounds-1;i>=0;i--){
System.out.println(""+i);
run();
}}catch(Exception e){e.printStackTrace();}
}

/*does one round of the algroithm.*/
void run(){
int crossoverchance=25;//percentage
/*raise this parameter if you think crossover is usefull.
set to zero if you only want mutation to happen*/
randomorder();
for(int i=0;i+1<popsize;i+=2){
pop[i]=winner(pop[i],pop[i+1]);
pop[i+1]=null;
}
for(int i=0;i+1<popsize;i+=4){
if(maybe(crossoverchance)){
weightvector[] r=pop[i].crossover(pop[i+2]);
pop[i+1]=r[0];
pop[i+3]=r[1];
}else{
pop[i+1]=new weightvector(pop[i]);
pop[i+3]=new weightvector(pop[i+2]);
}
}
pop[popsize-1]=new weightvector(pop);
}


void randomorder(){
for(int i=popsize-1;i>1;i--)
swap(i,rand(i+1));
}
/**
* swap to elements of the population.
*/
void swap(int a,int b){
weightvector dummy=pop[a];
pop[a]=pop;
pop[b]=dummy;
}


weightvector winner(weightvector w1,weightvector w2){
P[0].setweight(w1.a);
P[1].setweight(w2.a);
b.clear();
int onturn=0;
while(!b.finished()){
b.domove(P[onturn].bestmove());
if(onscreen)
bv.repaint();
onturn=1-onturn;
}
if(b.P1wins())
return w1;
return w2;
}


/*return a random integer between 0 and r (r not included)*/
int rand(int r)
{return (int)(Math.random()*r);}
/*returns true with the given chance*/
boolean maybe(int percent)
{return rand(100)<percent;}

/*clean up the frames that were made.*/
public void cleanup(){
if(onscreen){
bv.superframe.hide();
bv.superframe.dispose();
}
}


public static void main(String[] ps){
int pop=2;
int rounds=3;
genetictrain g=new genetictrain(pop);//make new set
//g.read("myoutput.txt");//remove this line if you have no inputs yet
g.run(rounds);//do some processing
g.write("newoutput.txt",rounds+" rounds");//write the output
g.cleanup();//remove the window (if any)
System.exit(0);
}

}



class weightvector{

static int n=2*board.evalvectorlength+1;
int[] a=new int[n];
String name;
static int[] min=new int[n];
static int[] max=new int[n];

static{
for(int i=0;i<n-1;i++){
min=-10;
max[i]=10;
}
min[n-1]=5;
max[n-1]=60;
}


public weightvector(){/*random initialisation*/
for(int i=0;i<n;i++)
a[i]=rand(min[i],max[i]+1);
name=randomname();
}

String randomname(){
String r="_";
for(int i=0;i<2;i++)
r+=(char)(rand(26)+'a');
return r;
}

public weightvector(weightvector w){/*mutated copy*/
for(int i=0;i<n;i++)
a[i]=bound(w.a[i]-1+rand(0,3),min[i],max[i]);
name="M"+w.name;
}

public weightvector(weightvector[] pop){
for(int i=0;i<n;i++)
a[i]=0;
for(int p=0;p<pop.length;p++)
for(int i=0;i<n;i++)
a[i]+=pop[p].a[i];
for(int i=0;i<n;i++)
a[i]=a[i]/pop.length;
name="avg";
}


public weightvector(String s){
StringTokenizer st=new StringTokenizer(s);
for(int i=0;i<n;i++)
a[i]=file.num(st.nextToken());
name=st.nextToken();
}
public String toString(){/*convert to string*/
String s=""+a[0];
for(int i=1;i<n;i++)
s+=" "+a[i];
s+=" "+name;
return s;
}

public weightvector[] crossover(weightvector w){
weightvector[] r={new weightvector(),new weightvector()};
for(int i=0;i<n;i++){
int x=rand(0,2);
r[x].a[i]=a[i];
r[1-x].a[i]=w.a[i];
}
String s=randomname();
r[0].name="X"+s;
r[1].name="x"+s;
return r;
}

int rand(int a,int b)//return random number r:a<=r<b
{return a+rand(b-a);}
int rand(int r)//return random number r:0<=x<r
{return (int)(Math.random()*r);}
int bound(int x,int min,int max){
if(x<min) return min;
if(x>max) return max;
return x;
}

}



class file{
BufferedReader in;
PrintWriter out;
boolean OK;

/**
* open a file for reading.
*/
public void readopen(String f1)
{readopen(new File(f1));}
/**
* open a file for reading.
*/

public void readopen(File f1){
try{
in=new BufferedReader(new FileReader(f1));
OK=true;
}catch(Exception e)
{e.printStackTrace();OK=false;}
}
/**
* read one line of the input file.
*/
public String read(){
if(!OK)
return null;
try{
return in.readLine().trim();
}catch(Exception e)
{e.printStackTrace();}
return null;
}

/**
* close the file opened for reading.
*/
public void readclose(){
try{
if(in!=null)in.close();
}catch(Exception e)
{e.printStackTrace();}
}

/**
* open a file for writing. Note that it is allowed to have both a file to
* read and a file to write with the same file object.
*/
public void writeopen(String f1)
{writeopen(new File(f1));}
/**
* open a file for writing. Note that it is allowed to have both a file to
* read and a file to write with the same file object.
*/

public void writeopen(File f1){
try{
out=new PrintWriter(new FileOutputStream(f1));
OK=true;
}catch(Exception e)
{e.printStackTrace();OK=false;}
}
/**
* Write out the given string as a separate line.
*/

public void write(String s)
{if(OK)out.println(s);}

/**
* close the writefile
*/
void writeclose()
{if(out!=null)out.close();}
/**
* close both the file opened for reading and the one for writing (if any)
*/
public void close()
{readclose();writeclose();}

/**
* converts a String to an int.
*/
public static int num(String s){
return Integer.parseInt(s.trim());
}

/**
* converts a String to a double.
*/
public static double dnum(String s){
return new Double(s).doubleValue();
}
}




import java.util.*;

import java.io.*;



class ClubLadder

{

private Vector <String> _ladder;


public ClubLadder()

{

_ladder = new Vector<String>();
}

// add and remove players from the ladder

public void addPlayer(String name)

{

if(_ladder != null)

_ladder.add(name);

}



public void removePlayer(String name)

{

if(_ladder != null)

_ladder.remove(name);

}



public void removePlayer(int rank)

{

if(_ladder != null)

_ladder.remove(rank-1);

}

// get player ranking from name and vice versa

public int getPlayerRank(String name)
{

if(_ladder != null)

return _ladder.indexOf(name)+1;

return -1;
}


public String getPlayerAt(int rank)
{

if(_ladder != null)
return _ladder.elementAt(rank-1);
return null;
}


// move someone up/down in the ladder
public void promote(String name, int levels)
{
int currentPosition, newPosition;

currentPosition = _ladder.indexOf(name);
newPosition = currentPosition - levels;


_ladder.remove(currentPosition);
_ladder.add(newPosition, name);
}

public int getLadderSize()
{
return _ladder.size();
}

public static ClubLadder loadLadder(String filename)throws IOException
{
ClubLadder ladder;
BufferedReader in;
String name;

ladder = new ClubLadder();

in = new BufferedReader(new FileReader(filename));
name = in.readLine();
while(in != null)
{
ladder.addPlayer(name);
name = in.readLine();
} // end while

return ladder;
}


public void saveLadder(String filename) throws IOException
{
PrintWriter out;
out = new PrintWriter(filename);
for(String playerName : _ladder)
{
out.println(playerName);
}
}

public String toString()
{
return "ClubLadder : " + _ladder.toString();
}

public static void main(String[] args)
{
ClubLadder foo;
String x, y;
x = "HelloWorld";
if(args.length !=0)
y = x.substring(0,5);
else
y = x.substring(5,10);

foo = new ClubLadder();

foo.addPlayer("HelloWorld");
foo.addPlayer("Hello");
foo.addPlayer("World");
//foo.add(y);

System.out.println(foo);
foo.removePlayer(y);
System.out.println(foo);


}
}




import java.util.*;

import java.awt.*;



class RobotPlayer

{

Counter _colour;

OthelloBoard _board;



public RobotPlayer(OthelloBoard b, Counter colour)

{

_colour = colour;

_board = b;

}



void makeMove()

{

Vector <Point> moves;

Point move;

moves = board.getValidMoves(colour);

move = moves.get(0);

board.makeMove(colour,
(int) move.getX(),
(int) move.getY());

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 18 2007
Added on Mar 21 2007
5 comments
370 views