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
- public static void main (String[] args)
- {
- c = new Console (10, 25);
- int[] [] board = new int [6] [7];
- int lRow = 0;
- int lColumn = 0;
- int win = 0;
- char nex = 'q';
- int curPos = 0;
- byte red = 1;
- 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)
- {
- refresh(c,board, curPos);
- nex = c.getChar ();
- if (nex == 'a')
- {
- curPos--;
- if (curPos < 0)
- {
- curPos = board.length;
- }
- }
- else if (nex == 'd')
- {
- curPos++;
- if (curPos >= board[0].length)
- {
- curPos = 0;
- }
- }
- else if (nex == 's')
- {
- if (findRow (board, curPos) != -1)
- {
- lColumn = curPos;
- lRow = findRow (board, curPos);
- board [lRow] [lColumn] = (int) red;
- red = (byte) (-(red));
- }
- }
- win = checkForWinner (board, lRow, lColumn);
- //c.println(win);
- }
- refresh(c,board, curPos);
- if (win == 1)
- {
- c.setTextColor(Color.RED);
- c.println("Red Win!");
- } else
- {
- c.setTextColor(Color.BLACK);
- c.println("Black 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 < 3 ; 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;
- }
- }
- 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++)
- {
- if (xx == curPos)
- {
- c.setTextColor(Color.MAGENTA);
- c.print ("V");
- c.setTextColor(Color.BLUE);
- }
- 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);
- }
- } // GroupAConnectFour class
Add Comment
Please, Sign In to add comment