Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The "GroupAConnectFour" class.
- import java.awt.*;
- import hsa.Console;
- public class GroupAConnectFour
- {
- static Console c; // The output console
- static Console cDebug;
- public static void main (String[] args)
- {
- c = new Console (10, 25);
- cDebug = new Console ();
- int[] [] board = new int [6] [7];
- int lRow = 0;
- int lColumn = 0;
- int win = 0;
- char nex = 'q';
- int curPos = 0;
- byte red = 1;
- boolean bro = false;
- int toPos = 0;
- boolean ai = false; //CHANGE DIS
- c.println ("Group A's: Connect Four");
- c.println ("A moves selector left, \n\tD right, \n\tand S drops.");
- c.println ("Press any key to continue");
- c.getChar ();
- clearBoard (board);
- c.setTextColor (Color.BLUE);
- while (win == 0 && bro == false)
- {
- refresh (c, board, curPos);
- if ((ai == true && red == 1) || (ai == false))
- {
- nex = c.getChar ();
- if (nex == 'a')
- {
- toPos = curPos - 1;
- if (toPos < 0)
- {
- toPos = board.length;
- }
- }
- else if (nex == 'd')
- {
- toPos = curPos + 1;
- if (toPos >= board [0].length)
- {
- toPos = 0;
- }
- }
- }
- else if (ai == true)
- {
- toPos = aiMove (board, cDebug);
- nex = 's';
- }
- while (toPos != curPos)
- {
- if (toPos < curPos)
- {
- curPos--;
- }
- else if (toPos > curPos)
- {
- curPos++;
- }
- }
- if (nex == 's')
- {
- if (findRow (board, curPos) != -1)
- {
- lColumn = curPos;
- lRow = findRow (board, curPos);
- board [lRow] [lColumn] = (int) red;
- red = (byte) (-(red));
- }
- }
- //cDebug.println(lRow + "\t" + checkForWinner (board, lRow, lColumn));
- win = checkForWinner (board, lRow, lColumn);
- //c.println(win);
- bro = true;
- for (int xx = 0 ; xx < board.length ; xx++)
- {
- for (int yy = 0 ; yy < board [0].length ; yy++)
- {
- if (board [xx] [yy] == 0)
- {
- bro = false;
- break;
- }
- }
- if (bro == false)
- {
- break;
- }
- }
- }
- refresh (c, board, curPos);
- if (win == 1)
- {
- c.setTextColor (Color.RED);
- c.println ("Red Win!");
- }
- else if (win == -1)
- {
- c.setTextColor (Color.BLACK);
- c.println ("Black Win!");
- }
- else
- {
- c.setTextColor (Color.BLUE);
- c.println ("Noone Win!");
- }
- } // main method
- public static int checkForWinner (int[] [] board, int lastRow, int lastColumn)
- {
- int totalInColumn = 0;
- int totalInRow = 0;
- int winCheck = 0;
- //Check for columns
- for (int i = 0 ; i < 3 ; i++)
- {
- int columnCheck = board [i] [lastColumn];
- for (int p = 1 ; p < 4 ; p++)
- columnCheck = columnCheck + board [i + p] [lastColumn];
- if (Math.abs (columnCheck) == 4)
- {
- winCheck = (board [lastRow] [lastColumn]);
- return winCheck;
- }
- }
- //Check for rows
- for (int i = 0 ; i < 4 ; i++)
- {
- int rowCheck = board [lastRow] [i];
- for (int p = 1 ; p < 4 ; p++)
- rowCheck = rowCheck + board [lastRow] [i + p];
- if (Math.abs (rowCheck) == 4)
- {
- winCheck = (board [lastRow] [lastColumn]);
- return winCheck;
- }
- }
- //Check for Diagonals
- for (int i = 1 ; i < 4 ; i++)
- {
- int diagonalCheck = board [lastRow] [lastColumn];
- // Row + 1 Column + 1
- if (lastRow + i < 6 && lastColumn + i < 7)
- {
- for (int x = 1 ; x < 4 ; x++)
- {
- if (lastRow + x < 6 && lastColumn + x < 7)
- diagonalCheck = diagonalCheck + board [lastRow + x] [lastColumn + x];
- cDebug.clear ();
- cDebug.println ((lastRow + i) + " " + " " + (lastColumn + i) + " " + diagonalCheck);
- }
- }
- //Row -1 Column - 1
- if (lastRow >= 3 && lastColumn >= 3)
- {
- for (int x = 1 ; x < 4 ; x++)
- {
- diagonalCheck = diagonalCheck + board [lastRow - x] [lastColumn - x];
- cDebug.clear ();
- cDebug.println ((lastRow + x) + " lastColumn + i=" + (lastColumn + x) + " diagonaCheck=" + diagonalCheck + " i=" + x);
- }
- }
- //Row -1 Column + 1
- if (lastRow - i > 0 && lastRow >= 3 && lastColumn + i < 7)
- {
- for (int x = 1 ; x < 4 ; x++)
- {
- diagonalCheck = diagonalCheck + board [lastRow - x] [lastColumn + x];
- cDebug.clear ();
- cDebug.println ((lastRow) + " " + " " + (lastColumn) + " " + diagonalCheck + " minus 1 plus 1");
- }
- }
- //Row + 1 Column - 1
- if (lastColumn - i > 0 && lastColumn >= 3 && lastRow + i < 7)
- {
- for (int x = 1 ; x < 4 ; x++)
- {
- diagonalCheck = diagonalCheck + board [lastRow + x] [lastColumn - x];
- cDebug.clear ();
- cDebug.println ((lastRow) + " " + " " + (lastColumn) + " " + diagonalCheck + " minus 1 plus 1");
- }
- }
- if (Math.abs (diagonalCheck) == 4)
- {
- winCheck = (board [lastRow] [lastColumn]);
- return winCheck;
- }
- }
- return 0;
- }
- private static int findRow (int[] [] board, int col)
- {
- int row = 0;
- for (int i = 0 ; i < board.length ; i++)
- {
- if (board [i] [col] == 0)
- {
- row = i;
- return row;
- }
- else
- {
- row = -1;
- }
- }
- return row;
- }
- public static void clearBoard (int[] [] board)
- {
- for (int xx = 0 ; xx < board.length ; xx++)
- {
- for (int yy = 0 ; yy < board [0].length ; yy++)
- {
- board [xx] [yy] = 0;
- }
- }
- }
- public static void refresh (Console c, int[] [] board, int curPos)
- {
- c.clear ();
- for (int xx = 0 ; xx < board [0].length ; xx++)
- {
- c.setTextColor (Color.GREEN);
- if (xx == curPos)
- {
- c.setTextColor (Color.MAGENTA);
- c.print ("V");
- c.setTextColor (Color.GREEN);
- }
- else
- {
- c.print ("~");
- }
- c.print (" ");
- }
- c.println ();
- for (int xx = board.length - 1 ; xx >= 0 ; xx--)
- {
- for (int yy = 0 ; yy < board [0].length ; yy++)
- {
- if (board [xx] [yy] == 1)
- {
- c.setTextColor (Color.RED);
- c.print ("O" + " ");
- }
- else if (board [xx] [yy] == -1)
- {
- c.setTextColor (Color.BLACK);
- c.print ("X" + " ");
- }
- else
- {
- c.setTextColor (Color.BLUE);
- c.print ("." + " ");
- }
- }
- c.println ();
- }
- c.setTextColor (Color.BLUE);
- }
- public static int aiMove (int[] [] board, Console c)
- {
- int toMove = 0;
- int preMove = 0;
- c.clear ();
- toMove = (int) (Math.random () * board.length);
- for (int xx = 0 ; xx < board [0].length ; xx++)
- {
- c.println (xx + "\t" + checkForWinner (board, findRow (board, xx), xx));
- if (checkForWinner (board, findRow (board, xx), xx) != 0)
- {
- toMove = xx;
- break;
- }
- }
- //toMove = (int)(Math.random()*board.length);
- return toMove;
- }
- } // GroupAConnectFour class
Add Comment
Please, Sign In to add comment