# 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;

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

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

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

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

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

if (n &gt; 4 && m &gt; 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 &lt;7; r++){
System.out.print(r +" ");
for (c = 0; c &lt;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 &lt;7; r++){
for (int c = 0; c &lt;7; c++){
if (board[r][c] == 1) {
count++;
}
}
}
count--;
return count;

}

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

Random generator = new Random();
int x = 0;
System.out.println();
System.out.print("Enter number of pegs
to randomly place on Peg Solitare board &gt;&gt; ");
System.out.flush();
if (x &gt; maxpegs){

System.out.println("Pegs amount greater than maximum allowed for this
x = maxpegs;
}

System.out.println();

//if (x == 0 )

int r = 0;
int c = 0;
while (x &gt; 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;
Random generator = new Random();

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

System.out.println("Please enter move, seperated by spaces");
}
}

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

// iterates thorugh the ... something?+
System.out.println("Testing now...");
for (int s = 1; s &lt; 4; s++)
{
// System.out.println("Testing entry "+s+" now...");
int z[] = new int;
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 &lt;= 4;
test_loop++ )
{

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

int Valid = 0;
int x1 = z;
int y1 = z;
int x2 = z;
int y2 = z;
int xvec = 1;
int yvec = 1;
if( x1 &gt; x2 )
{
xvec *= -1;
}
if( y1 &gt; 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 &gt;
1 || x1 &lt; 5 ) && ( y1 &lt; 2 || y1 &gt; 4 ) ) || ( y1 &gt; 1
&& y1 &lt; 5 ) ) &&
( ( ( x2 &gt;
1 || x2 &lt; 5 ) && ( y2 &lt; 2 || y2 &gt; 4 ) ) || ( y2 &gt; 1
&& y2 &lt; 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
}
// 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();
}

}
``````