Boredom strikes again!

Ok, Ok, I have to stop doing people’s homework for them… but this one was a challenge!

/*
** file: Pegs.java
** created: 02/04/05
** author: Craig Lester
*/

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.lang.Math.*;

public class Pegs {

int matches = 15;
int player = 2;
int remnum;

int board[][] = new int[9][9];

public void setupboard() {
int r = 0;
int c = 0;

for (int n = 0; n < 7; n++) {
for (int m = 0; m < 7; m++) {
board[n][m] = 1;

if (n < 2 && m < 2){
board[n][m] = 0;
}

if (n < 2 && m > 4){
board[n][m] = 0;
}

if (n > 4 && m < 2){
board[n][m] = 0;
}

if (n > 4 && m > 4){
board[n][m] = 0;
}
}
}

}

public void drawboard() {
int r = 0;
int c = 0;

System.out.println(" 0 1 2 3 4 5 6");
for (r = 0; r <7; r++){
System.out.print(r +" ");
for (c = 0; c <7; c++){
switch (board[r][c]){
case 1:
System.out.print(". ");
break;
case 2:
System.out.print("* ");
break;
default:

System.out.print(" ");

}

}
System.out.println();
}

}

public int countspots(){
int count = 0;
for (int r = 0; r <7; r++){
for (int c = 0; c <7; c++){
if (board[r][c] == 1) {
count++;
}
}
}
count--;
return count;

}

public void placepegs() throws IOException {
int maxpegs = countspots();

BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
Random generator = new Random();
int x = 0;
System.out.println();
System.out.print("Enter number of pegs
to randomly place on Peg Solitare board >> ");
System.out.flush();
x = Integer.parseInt(in.readLine());
if (x > maxpegs){

System.out.println("Pegs amount greater than maximum allowed for this
board, using maximum instead");
x = maxpegs;
}

System.out.println();

//if (x == 0 )

int r = 0;
int c = 0;
while (x > 0){
r = generator.nextInt(7);
c = generator.nextInt(7);
if (board[r][c] == 1){
board[r][c] = 2;
x--;
}

}

}

public void play() throws IOException {
String y[] = new String[7];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Random generator = new Random();

y[0] = " ";
// asks for the user to input four moves
for (int r = 1; r < 4; r++){
if (y[r-1] != ""){

System.out.println("Please enter move, seperated by spaces");
y[r] = in.readLine();
}
}

for( int test_loop = 1; test_loop < 4; test_loop++ )
{
System.out.println(y[test_loop]);
}

// iterates thorugh the ... something?+
System.out.println("Testing now...");
for (int s = 1; s < 4; s++)
{
// System.out.println("Testing entry "+s+" now...");
int z[] = new int[5];
if (y[s] != null)
{
//System.out.println("DEBUG " +y[s]);

StringTokenizer ts = new
StringTokenizer(y[s]);
// goes through each token in the string

for( int test_loop = 1; test_loop <= 4;
test_loop++ )
{

z[test_loop] =
Integer.parseInt(ts.nextToken());
}
//System.out.print(z[s]);
}

int Valid = 0;
int x1 = z[1];
int y1 = z[2];
int x2 = z[3];
int y2 = z[4];
int xvec = 1;
int yvec = 1;
if( x1 > x2 )
{
xvec *= -1;
}
if( y1 > y2 )
{
yvec *= -1;
}
System.out.println( "\nTesting movement from "+x1+","+y1+" to "+x2+","+y2 );
// has the start position got a peg in it, and is the move a valid spot?
if( board[x1][y1] == 2 && (
( ( ( x1 >
1 || x1 < 5 ) && ( y1 < 2 || y1 > 4 ) ) || ( y1 > 1
&& y1 < 5 ) ) &&
( ( ( x2 >
1 || x2 < 5 ) && ( y2 < 2 || y2 > 4 ) ) || ( y2 > 1
&& y2 < 5 ) ) ) )
{
// calculate the movement vector
int movex = Math.abs( x1 - x2 );
int movey = Math.abs( y1 - y2 );
// is it moving two spaces?
if( ( movex ==
2 && movey == 0 ) || ( movex == 0 && movey == 2 ) )
{
//System.out.println("movement checked out");
// if the destination is clear
if( board[x2][y2] == 1 )
{

//System.out.println( "destination is clear" );
// if there is a peg to jump over

// this is where it's jumping over

int checkjumpx = x1 + ( Math.abs( movex / 2 )
* xvec );

int checkjumpy = y1 + ( Math.abs( movey / 2 )
* yvec );

//System.out.println( "check spot :
"+checkjumpx+","+checkjumpy);

// check if the spot it's jumping is a peg

if( board[checkjumpx][checkjumpy] == 2 )
{

System.out.println( "Valid"
);
Valid = 1;
}
}
// if the destination has a peg in it
else if( board[x2][y2] == 2 )
{

//System.out.println( "Destination has a peg
in it already" );
}
// if the destination is off board
else
{

//System.out.println( "Destination off board"
);
}
}
// if it's moving too far
else
{
//System.out.println("x movement was: "+movex );
//System.out.println("y movement was: "+movey );
}
}
// if there was no peg to grab in the first place
else
{
//
System.out.println( "no peg at "+x1+" "+y1+" value was "+board[x1][y1]
);

//System.out.println( "invalid move, either peg not there, or out
of bounds" );
}

if (Valid == 0)
{

System.out.println("Invalid");
}

}

}

public void game() {
System.out.println ("Java Pegs Game");
System.out.println ("=============");
System.out.println ();
setupboard();
try {
placepegs();
}
catch (IOException i) {
System.out.println("Erronous Input");
}

drawboard();
try {
play();
}
catch (IOException i) {
System.out.println("Erronous Input");
}
drawboard();

}

public static void main(String[] args) throws IOException {
Pegs tpo = new Pegs();
tpo.game();
}

}

 



#Programming