import java.io.*; /** Connect4.java Written by: Stuart Hansen Date: August 30, 2003 Purpose: This file contains a program that plays the game Connect 4. */ public class Connect4 { /** This method prints who has won the game */ public static void printResult (Position pp) { if (pp.getWinner() == ComputerPlayer.COMPUTER_CHARACTER) System.out.println("Computer wins big time!!"); else if (pp.getWinner() == HumanPlayer.HUMAN_CHARACTER) System.out.println("You got lucky and won this time!!"); else System.out.println("The game was a draw!"); } /** The main program drives the game */ public static void main (String [] args) throws Exception { Position p = new Position(); p.print(); HumanPlayer human = new HumanPlayer(); char user_first; /* flag to see who goes first */ int strength; /* See how strong of game the user want to play */ if (args.length > 0) { strength = Integer.parseInt(args[0]); if (strength < 1) strength = 1; } else strength = 5; ComputerPlayer computer = new ComputerPlayer(strength); /* Check to see who plays first */ System.out.print("Do you want to go first (Y or N) -->"); BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in)); String line = bufReader.readLine(); user_first = line.charAt(0); if (user_first == 'Y' || user_first == 'y') { p=human.move(p); p.print(); } /* Continue playing until the game is over */ while (!p.gameOver()) { p = computer.move(p); p.print(); if (!p.gameOver()) { p=human.move(p); p.print(); } } printResult(p); } }