Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Loops giving wrong output

$
0
0
Hey guys, new to this website, and fairly new to java. Have an assignment here. I have a correct output for my first output, which is print out the possible number of times a red checker ca jump a black checker. What I am having problems with is my second output. I am trying to print out how many times a red checker has the oppurtunity to perform a "double jump" my loops seem right but it gives an output of two on this infile named "test1.txt."
R_R___R_
_B_R_R_R
__R___B_
________
____R_R_
_B_R_B_B
B_B_R___
___B_B_B

Here is my code. I also have a class named Checker that gets and sets col and row by [i][j]. Using eclipse. Thanks again.
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;




public class Checkerboard
{
	/**
	 * @param args
	 * Initializes a 2d array, a single array, and four integer variables.
	 */
		static char [][] checkerboard = new char [8] [8];
		static Checker [] reds = new Checker[12];
		static int redcounter;
		static int possibleJumps;
		static int doubleJumps;
		static Checker[] dubs = new Checker[12];
		static int dubscounter;
	
		/**
		 * reads in the infile and stores the 64 characters into the 8x8 2d array.
		 * @param infile
		 */
		public void readFile(Scanner infile) 
		{
			for(int row = 0; row< checkerboard.length; row++)
			{
				String value = infile.nextLine();
				
				for(int col = 0 ; col<value.length();col++)
				{
					checkerboard [row] [col] = value.charAt(col);
					
				}
			}
			//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
			//STORE IT INTO THE 2-D ARRAY FOR TASK #2
			infile.close();
			
		}
		
		/**
		 * creates the empty 8x8 array
		 */
	public Checkerboard()
	{
		checkerboard = new char [8] [8];
		
	}
	
	/**
	 * the method counts the possible jumps black can jump red given the conditions. it then stores the value in recounter.
	 * @return returns the counter and stores the counter value in possibleJums
	 */
	public static int countJumps()
	{
		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j<8; j++)
			{
				if(checkerboard[i][j] == 'R')
				{
					Checker red = new Checker(i,j); 
					reds[redcounter] = red;
					redcounter++;
				}
			
			}
		}
		
		for(int i = 0; i < reds.length;i++)
		{
			if (reds[i] != null)
			{	
				int row = reds[i].getRow();
				int col = reds[i].getCol();
				
				if(col>1 && row<6)
				{
					
					if(checkerboard[row+1][col-1]=='B'&& checkerboard[row+2][col-2] == '_')
				
					{
						possibleJumps++;
					}
				}
				if(col<6 && row<6)	
				{
					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
					{
						possibleJumps++;
					}
				}
			}
		}
		return possibleJumps;
	}
	
	
	public static int countDouble()
	{
		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j<8; j++)
			{
				if(checkerboard[i][j] == 'R')
				{
					Checker dub = new Checker(i,j); 
					dubs[dubscounter] = dub;
					dubscounter++;
				}
			
			}
		}
		for(int i = 0; i < dubs.length;i++)
		{
			if (dubs[i] != null)
			{	
				int row = dubs[i].getRow();
				int col = dubs[i].getCol();
				
				if(col<6 && row<4)
				{
					//check for right and left double jump
					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
				
					{
						if(checkerboard[row+3][col+1]=='B'&&checkerboard[row+4][col]=='_')
						doubleJumps++;
					}
				
				}
				if( col < 4 && row < 4)
				{
					//check for right and right double jump
					if(checkerboard[row+1][col+1]=='B'&&checkerboard[row+2][col+2]=='_')
					{
						if(checkerboard[row+3][col+3]=='B'&&checkerboard[row+4][col+4]=='_')
							doubleJumps++;
					}
				}
				
				if( col>3 && row<4)
				//left left double jump
				{
					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
					{
						if(checkerboard[row+3][col-3]=='B'&&checkerboard[row+4][col-4]=='_')
							doubleJumps++;
					
					 }
			
				}
		
				if(col>1 &&row<4)
				{
					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
					{
						if(checkerboard[row+3][col-1]=='B'&& checkerboard[row+4][col]=='_');
							doubleJumps++;
					}
			
				}
			}
		}
		return doubleJumps;
	}

	

		
	/**
	 * Main method creates a scanner and asks the user for the infile name.
	 * creates the new obeject board and prints the array aswell as a return statement of possible jumps.
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException
	
	{
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);
		System.out.print("Please enter the file configuration name: " );
		String input = in.nextLine();
		Checkerboard board = new Checkerboard();
		File file  = new File(input);
		Scanner infile = new Scanner(file);
		board.readFile(infile);
		for(int i = 0; i<8; i++)
		{
			for(int j = 0; j<8; j++)
			{
				System.out.print(checkerboard[i][j]);
			}
			System.out.println();
		}	
		countJumps();
		System.out.println("There are "+possibleJumps + " possible moves for Red that will result in at least one checker being captured.");
		System.out.println();
		countDouble();
		System.out.println("There are "+doubleJumps +" possible double jump moves for Red.");
	} 
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>