so much code...
I appologize for the very few (Ian :)) that care, but I obviously havnt updated this as of late.
lots of exciting (well, not really) stuff/pictures to share though. I'm just lazy.
anyway, its about 6 AM, just got done with this.
Well, its not done done, but its a damn good start.
import java.util.Random;
public class ai
{
public static void main( String args[] )
{
//get width of game board
//set playerToken, cpuToken, and blankSquare variables to proper values depending on martins implementation
//set following variables:
int needed = 4; //number of tokens needed in order to win
int width = 7; //the width of the token array
int height = 3; //the height of the token array
int playerToken = 2; //the intiger value that reprisents the player's token
int cpuToken = 1; //the intiger value that reprisents the CPU's token
int blankSquare = 0; //the int value that represents a blank/empty square
/***************************************************************************************
this section is for test purposes
***************************************************************************************/
int[][] array = new int[width][height];
array[1][1]=9;
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++)
array[i][j] = 0;
/* //one test
array[0][0] = cpuToken;
array[0][1] = cpuToken;
array[1][0] = playerToken;
array[1][1] = cpuToken;
array[2][0] = playerToken;
array[2][1] = cpuToken;
array[2][2] = playerToken;
array[3][0] = cpuToken;
array[5][0] = cpuToken;
array[5][1] = playerToken;
array[6][2] = playerToken;
array[6][1] = cpuToken;
array[6][0] = playerToken;
*/
//two test
array[1][0] = playerToken;
array[1][1] = playerToken;
array[0][0] = playerToken;
array[2][0] = playerToken;
/***************************************************************************************
end test
***************************************************************************************/
class spot
{
public int x = 1000;
public int y = 1000;
public int maxPlayer = -1;
public int maxCPU = -1;
public int playable = -1;
}
spot[] where = new spot[width];
for( int i = 0; i < width; i++ )
{
where[ i ] = new spot();
}
// This code sets up each spot object in the "where" array with the first location (x,y) of an empty position
// that the computer can play from. If the algorythm completes and no empty spot is found, -1 is store in the
// y value.
for( int i = 0; i < width; i++ )
{
where[i].x = i;
for( int j = 0; j < height; j++ )
{
if( array[ i ][ j ] == blankSquare )
{
where[ i ].y = j;
j = height;
}
}
}
int teamToken=1000, temp=0, xPos=0, yPos=0;
boolean sameToken=true;
for( int i = 0; i < width; i++ ) // Start main loop
{
if( where[ i ].y < ( height ) ) //only checks spaces that a token can be placed in.
{
where[ i ].playable = 1;
where[ i ].maxCPU = 0;
where[ i ].maxPlayer = 0;
//Begin tests
// ***********************************************************************************Testing North-East direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x + 1 ) < width ) && ( ( where[ i ].y + 1 ) < height ) )
{
xPos = ( where[ i ].x + 1 );
yPos = ( where[ i ].y + 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos < width ) && ( yPos < height ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos++;
yPos++;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//*********************************************************************************** Testing East direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( where[ i ].x + 1 ) < width )
{
xPos = ( where[ i ].x + 1 );
yPos = ( where[ i ].y );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos < width ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos++;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//***********************************************************************************Test South - East direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x + 1 ) < width ) && ( ( where[ i ].y - 1 ) > 0 ) )
{
xPos = ( where[ i ].x + 1 );
yPos = ( where[ i ].y - 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos < width ) && ( yPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos++;
yPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//*******************************************************************************Test South direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( where[ i ].y - 1 ) >= 0 )
{
xPos = ( where[ i ].x );
yPos = ( where[ i ].y - 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( yPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
yPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//*********************************************************************************Testing South-West direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x - 1 ) >= 0 ) && ( ( where[ i ].y - 1 ) >= 0 ) )
{
xPos = ( where[ i ].x - 1 );
yPos = ( where[ i ].y - 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos >= 0 ) && ( yPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos--;
yPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//***********************************************************************************Testing West direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( where[ i ].x - 1 ) >= 0 )
{
xPos = ( where[ i ].x - 1 );
yPos = ( where[ i ].y );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//testing North-West direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x - 1 ) >= 0 ) && ( ( where[ i ].y + 1 ) < height ) )
{
xPos = ( where[ i ].x - 1 );
yPos = ( where[ i ].y + 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos >= 0 ) && ( yPos < height ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos--;
yPos++;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//END OF TESTS
}
}
//Choose where CPU drops based on following:
// if player is N-1 away from winning, drop there.
// else, drop at its maximum place
// if there is no maximum, pick random spot.
int maxPlayer = 0, maxCPU = 0, localMaxCPU = -1, localMaxPlayer = -1;
Random generator = new Random();
for( int i = 0; i < width; i++ )
{
if( where[ i ].maxPlayer > maxPlayer )
{
maxPlayer = where[ i ].maxPlayer;
localMaxPlayer = i;
}
if( where[ i ].maxCPU > maxCPU )
{
maxCPU = where[ i ].maxCPU;
localMaxCPU = i;
}
}
if( maxCPU == ( needed - 1) )
System.out.println("1 from maxCPU -> " + localMaxCPU);//return( localMaxCPU );
else if( maxPlayer == ( needed - 1 ) )
System.out.println("1 from maxPlayer -> " + localMaxPlayer);//return( localMaxPlayer );
else
{
int[ ] randomCPU = new int[ width ];
int numberMax = 0, pos = 0, finalPos = 0;
for( int i = 0; i < width; i++ )
randomCPU[ i ] = -1;
for( int i = 0; i < width; i++ )
if( where[ i ].maxCPU == maxCPU )
{
randomCPU[ pos ] = i;
pos++;
}
finalPos = randomCPU[ generator.nextInt( pos ) ];
//return( finalPos );
System.out.println( "final: " + finalPos );
}
}
}
lots of exciting (well, not really) stuff/pictures to share though. I'm just lazy.
anyway, its about 6 AM, just got done with this.
Well, its not done done, but its a damn good start.
import java.util.Random;
public class ai
{
public static void main( String args[] )
{
//get width of game board
//set playerToken, cpuToken, and blankSquare variables to proper values depending on martins implementation
//set following variables:
int needed = 4; //number of tokens needed in order to win
int width = 7; //the width of the token array
int height = 3; //the height of the token array
int playerToken = 2; //the intiger value that reprisents the player's token
int cpuToken = 1; //the intiger value that reprisents the CPU's token
int blankSquare = 0; //the int value that represents a blank/empty square
/***************************************************************************************
this section is for test purposes
***************************************************************************************/
int[][] array = new int[width][height];
array[1][1]=9;
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++)
array[i][j] = 0;
/* //one test
array[0][0] = cpuToken;
array[0][1] = cpuToken;
array[1][0] = playerToken;
array[1][1] = cpuToken;
array[2][0] = playerToken;
array[2][1] = cpuToken;
array[2][2] = playerToken;
array[3][0] = cpuToken;
array[5][0] = cpuToken;
array[5][1] = playerToken;
array[6][2] = playerToken;
array[6][1] = cpuToken;
array[6][0] = playerToken;
*/
//two test
array[1][0] = playerToken;
array[1][1] = playerToken;
array[0][0] = playerToken;
array[2][0] = playerToken;
/***************************************************************************************
end test
***************************************************************************************/
class spot
{
public int x = 1000;
public int y = 1000;
public int maxPlayer = -1;
public int maxCPU = -1;
public int playable = -1;
}
spot[] where = new spot[width];
for( int i = 0; i < width; i++ )
{
where[ i ] = new spot();
}
// This code sets up each spot object in the "where" array with the first location (x,y) of an empty position
// that the computer can play from. If the algorythm completes and no empty spot is found, -1 is store in the
// y value.
for( int i = 0; i < width; i++ )
{
where[i].x = i;
for( int j = 0; j < height; j++ )
{
if( array[ i ][ j ] == blankSquare )
{
where[ i ].y = j;
j = height;
}
}
}
int teamToken=1000, temp=0, xPos=0, yPos=0;
boolean sameToken=true;
for( int i = 0; i < width; i++ ) // Start main loop
{
if( where[ i ].y < ( height ) ) //only checks spaces that a token can be placed in.
{
where[ i ].playable = 1;
where[ i ].maxCPU = 0;
where[ i ].maxPlayer = 0;
//Begin tests
// ***********************************************************************************Testing North-East direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x + 1 ) < width ) && ( ( where[ i ].y + 1 ) < height ) )
{
xPos = ( where[ i ].x + 1 );
yPos = ( where[ i ].y + 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos < width ) && ( yPos < height ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos++;
yPos++;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//*********************************************************************************** Testing East direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( where[ i ].x + 1 ) < width )
{
xPos = ( where[ i ].x + 1 );
yPos = ( where[ i ].y );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos < width ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos++;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//***********************************************************************************Test South - East direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x + 1 ) < width ) && ( ( where[ i ].y - 1 ) > 0 ) )
{
xPos = ( where[ i ].x + 1 );
yPos = ( where[ i ].y - 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos < width ) && ( yPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos++;
yPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//*******************************************************************************Test South direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( where[ i ].y - 1 ) >= 0 )
{
xPos = ( where[ i ].x );
yPos = ( where[ i ].y - 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( yPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
yPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//*********************************************************************************Testing South-West direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x - 1 ) >= 0 ) && ( ( where[ i ].y - 1 ) >= 0 ) )
{
xPos = ( where[ i ].x - 1 );
yPos = ( where[ i ].y - 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos >= 0 ) && ( yPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos--;
yPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//***********************************************************************************Testing West direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( where[ i ].x - 1 ) >= 0 )
{
xPos = ( where[ i ].x - 1 );
yPos = ( where[ i ].y );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos >= 0 ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos--;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//testing North-West direction
temp = 0;
teamToken = 1000;
sameToken = true;
if( ( ( where[ i ].x - 1 ) >= 0 ) && ( ( where[ i ].y + 1 ) < height ) )
{
xPos = ( where[ i ].x - 1 );
yPos = ( where[ i ].y + 1 );
teamToken = array[ xPos ][ yPos ];
if( teamToken == blankSquare )
{
sameToken = false;
teamToken = 1000;
}
while( ( xPos >= 0 ) && ( yPos < height ) && ( sameToken ) )
{
if( array[ xPos ][ yPos ] == teamToken )
{
temp++;
xPos--;
yPos++;
}
else
sameToken = false;
}
}
if( teamToken == playerToken )
if( temp > where[ i ].maxPlayer )
where[ i ].maxPlayer = temp;
if( teamToken == cpuToken )
if( temp > where[ i ].maxCPU )
where[ i ].maxCPU = temp;
//END OF TESTS
}
}
//Choose where CPU drops based on following:
// if player is N-1 away from winning, drop there.
// else, drop at its maximum place
// if there is no maximum, pick random spot.
int maxPlayer = 0, maxCPU = 0, localMaxCPU = -1, localMaxPlayer = -1;
Random generator = new Random();
for( int i = 0; i < width; i++ )
{
if( where[ i ].maxPlayer > maxPlayer )
{
maxPlayer = where[ i ].maxPlayer;
localMaxPlayer = i;
}
if( where[ i ].maxCPU > maxCPU )
{
maxCPU = where[ i ].maxCPU;
localMaxCPU = i;
}
}
if( maxCPU == ( needed - 1) )
System.out.println("1 from maxCPU -> " + localMaxCPU);//return( localMaxCPU );
else if( maxPlayer == ( needed - 1 ) )
System.out.println("1 from maxPlayer -> " + localMaxPlayer);//return( localMaxPlayer );
else
{
int[ ] randomCPU = new int[ width ];
int numberMax = 0, pos = 0, finalPos = 0;
for( int i = 0; i < width; i++ )
randomCPU[ i ] = -1;
for( int i = 0; i < width; i++ )
if( where[ i ].maxCPU == maxCPU )
{
randomCPU[ pos ] = i;
pos++;
}
finalPos = randomCPU[ generator.nextInt( pos ) ];
//return( finalPos );
System.out.println( "final: " + finalPos );
}
}
}


0 Comments:
Post a Comment
<< Home