Keeping an int value the same

Hey,

I am fairly new to java, and I take it at school. I am working on an AI for a tictactoe board. My problem is that I need to somehow initalize an integer based on certain conditions, and then I need to keep that integer the same throughout the game. The integer is named "me" its in the code posted below.

Thanks in advance!

class Kuivilaextends Player

{

public Kuivila(String name)

{

super(name);//call superclass constructor

}

publicint getMove(int[] board,int whoami)

{

int s;

int whoareyou;

if (whoami==1)

whoareyou=2;

//if (whoami==2)

else

whoareyou=1;

finalint oCorner = 0;

finalint oEdge = 1;

finalint oCenter = 2;

int me==99;

if (whoami==2)

return Defence(board,whoami,whoareyou);

/// otherwise

if (me!=oCorner && me!=oEdge && me!=oCenter)

{if (board[0]==whoareyou || board[2]==whoareyou || board[6]==whoareyou || board[8]==whoareyou)

me = oCorner;

if (board[1]==whoareyou || board[3]==whoareyou || board[5]==whoareyou || board[7]==whoareyou)

me = oEdge;

else

me = oCenter;

}

/// then

if (whoami==1)

return Offence(board,whoami,whoareyou, oCorner, oEdge, oCenter, me);

return 0;

///End Basic Maneuvers//////////////////////////

}

///Defence//////////////////////////////////////

publicint Defence(int[] board,int whoami,int whoareyou)

{

/// More Code Here

}

publicint Offence(int[] board,int whoami,int whoareyou,int oCorner,int oEdge,int oCenter,int me)

{

//int m1=oCorner;

if (board[6]==OPEN)

return 6;

/// If PLAYER2 Goes Corner ///

if (me==oCorner)

{

/// More Code Here

}

/// If PLAYER2 Goes Edge ///

if (me==oEdge && board[4]==OPEN)

{

/// More Code Here

}

if (me==oEdge)

{

/// More Code Here

}

}

}

[4472 byte] By [Parke-a] at [2007-11-26 13:19:41]
# 1

I guess you want something like this in C++,

public int Offence(int[] board, int whoami, int whoareyou,int oCorner,int oEdge,int oCenter,int me = 2)

I can tell you you need to overload the mothod,

Your actual method:

public int Offence(int[] board, int whoami, int whoareyou,int oCorner,int oEdge,int oCenter,int me)

Your other method:

public int Offence(int[] board, int whoami, int whoareyou,int oCorner,int oEdge,int oCenter) {

return Offence(board, whoami, whoareyou,oCorner,oEdge,oCenter,2);

}

Please let me know If this is not what you wanted.

reflex2javaa at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 2
Oops!make 'me' a static variable
reflex2javaa at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 3

What I'm trying to get is:

I want the code to check the first move of the opponent. THis is done through 'me' . 'me' starts at 99, just as a random number. The first time the class is invoked it will go to

if (me!=oCorner && me!=oEdge && me!=oCenter)

{ if (board[0]==whoareyou || board[2]==whoareyou || board[6]==whoareyou || board[8]==whoareyou)

me = oCorner;

if (board[1]==whoareyou || board[3]==whoareyou || board[5]==whoareyou || board[7]==whoareyou)

me = oEdge;

else

me = oCenter;

}

and change 'me' to either 0,1 or 2. The problem is, when the class is called on again it redefines 'me' as 99. I need a way to keep it at 0,1 or 2 once it has been changed from 99.

Parke-a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 4

Keep the variable static,

static {

public int me = Something.someRandomFunction();

}

The above should be at the class scope.

The static blocks gets executed only once. :-)

reflex2javaa at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 5
sorry, but im new at java and im not sure what a class scope is could you please explain?
Parke-a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 6

> sorry, but im new at java and im not sure what a

> class scope is could you please explain?

Scoping in java means boundaries... like this:

public class MyClass{ // <<-- start of class scope

public static void main(String args[]){ // <<-- start of Main() method scope

for(int i=0; i<5; i++){ // <<-- start of for loop scope

// do something here...

}// end of for loop scope

} // end of main method scope

} // end of class scope

angeles1016a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 7

Ok i did the following, but it says its an illegal start of expression when I compile?

class Kuivila extends Player

{

static {

public int me=99;

}

public Kuivila(String name)

{

super(name); //call superclass constructor

}

public int getMove(int[] board, int whoami)

{

int s;

int whoareyou;

if (whoami==1)

whoareyou=2;

//if (whoami==2)

else......blah blah blah

Parke-a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 8
http://java.sun.com/docs/books/tutorial/java/index.html
John60a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 9
John60-Thanks for the guide, but I have no idea where to look for an answer in it.
Parke-a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 10
you should start on the tutorial first.your error might be a missing '{', '}' but study the tutorial first.
angeles1016a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 11
ill check the tutorial, but just to let you know, the error is on line 4 and its mad about me using 'public'
Parke-a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 12

> static {

>public int me=99;

> }

There's a syntax error in the above code.

public class Xxxx {

// You can use public or private here

private int i;

public int p;

public static int s;

static {

// You can't use public or private here,

}

public void Mmmm() {

// you can't use public or private here either.

}

}

Message was edited by:

John60

John60a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 13

> your error might be a missing '{', '}'

The error is that he's trying do declare an attribute in the static initializer block. A non-static one with that, too.

@ OP: instead of that static block, put in there:

static private int me = 99;

CeciNEstPasUnProgrammeura at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 14
static private int me = 99; worked.Thanks everyone for your help!-Parker
Parke-a at 2007-7-7 17:46:48 > top of Java-index,Java Essentials,Java Programming...