Original Post
I've been teaching myself Java for about 4 months now, and I'm stuck. I'm trying to write connect 4 using the console, and instead of a player vs. a computer, I want it to be able to read in a text file and print out the results based on the text file. The input from the text file is as follows 1 0 4 5 3 6 Where the numbers equal which column the piece belongs in. I've gotten to the point where I have read in the file, but when I try to print out the results of the game, I can't get the pieces to stack up properly. Here's what I have so far... test.txt 0 0 0 1 1 1 2 2 4 3 3 5 6 6 5 6 4 6 3 6 Thanks much [Edited by - kyokai on June 10, 2007 5:20:24 PM]
import java.io.*;
import java.util.*;
public class Connect4Processor {
public Connect4Processor() {
}
ArrayList moves = new ArrayList();
//Ints repsresenting the height of each of the columns
int c0 = 5;
int c1 = 5;
int c2 = 5;
int c3 = 5;
int c4 = 5;
int c5 = 5;
int c6 = 5;
public void startup() throws FileNotFoundException {
Scanner in = new Scanner(new File("test.txt"));
Scanner in2 = new Scanner(new File("test2.txt"));
Scanner in3 = new Scanner(new File("test3.txt"));
System.out.println(
"Please insert the name of the file you would like to input");
while (in.hasNext()) {
moves.add(in.next());
}
in.close();
in2.close();
in3.close();
// System.out.println("0123456");//prints out the game board
}
public void board() {
System.out.println("");
System.out.println("0123456");
}
public void move() throws FileNotFoundException {
Connect4Processor c4p = new Connect4Processor();
for (int i = 0; i < moves.size(); i++) {
//System.out.print("" + i);
if (moves.get(i).equals("0")) {
c4p.board();
for (int j = 0; j < c0; j++) {
System.out.println("");
}
System.out.println("0");
c0--;
}
if (moves.get(i).equals("1")) {
c4p.board();
for (int j = 0; j < c1; j++) {
System.out.println("");
}
System.out.println(" 1");
c1--;
}
if (moves.get(i).equals("2")) {
c4p.board();
for (int j = 0; j < c2; j++) {
System.out.println("");
}
System.out.println(" 2");
c2--;
}
if (moves.get(i).equals("3")) {
c4p.board();
for (int j = 0; j < c3; j++) {
System.out.println("");
}
System.out.println(" 3");
c3--;
}
if (moves.get(i).equals("4")) {
c4p.board();
for (int j = 0; j < c4; j++) {
System.out.println("");
}
System.out.println(" 4");
c4--;
}
if (moves.get(i).equals("5")) {
c4p.board();
for (int j = 0; j < c5; j++) {
System.out.println("");
}
System.out.println(" 5");
c5--;
}
if (moves.get(i).equals("6")) {
c4p.board();
for (int j = 0; j < c6; j++) {
System.out.println();
}
c6--;
System.out.println(" 6");
}
}
System.out.println(moves);
System.out.println("" + c0 + "" + c1 + "" + c2 + "" + c3 + "" + c4 + "" +
c5 + "" + c6);
}
public static void main(String[] args) throws FileNotFoundException {
Connect4Processor c4p = new Connect4Processor();
c4p.startup();
c4p.move();
}
}