Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package package1;
- import javax.swing.JOptionPane;
- public class SurroundGame {
- private int cellBoardRow = 10, cellBoardColumn = 10, players = 2, currentPlayer = 1, startingPlayer = 1;
- private int player1Score = 0, player2Score = 0, player3Score = 0, player4Score = 0, player5Score = 0;
- private int player6Score = 0, player7Score = 0, player8Score = 0, player9Score = 0, player10Score = 0;
- private String status;
- private String name1, name2, name3, name4, name5, name6, name7, name8, name9, name10;
- public Cell[][] cellBoard;
- public SurroundGame() {
- // Asks the user for the number of rows that the cellBoard will have.
- String string1 = JOptionPane.showInputDialog("How many rows should the cellBoard have?:"
- + "\n(NOTE: Non-numeric characters will be ignored)");
- // Removes any non-numeric values.
- string1 = string1.replaceAll("[^0-9.]", "");
- // So long as an integer between 3 and 20 is entered, the cellBoard will have the number of rows entered.
- // Otherwise, the number of rows is set to 10.
- if (string1.equals("") == true || Integer.parseInt(string1) < 3 || Integer.parseInt(string1) > 20){
- JOptionPane.showMessageDialog (
- null, "Please enter an integer between 3 and 20 for the number of rows.\nRows set to 10.",
- "Number Error",
- JOptionPane.ERROR_MESSAGE);
- cellBoardRow = 10;
- }else {
- cellBoardRow = Integer.parseInt(string1);
- }
- // Asks the user for the number of columns that the cellBoard will have.
- String string2 = JOptionPane.showInputDialog("How many columns should the cellBoard have?:"
- + "\n(NOTE: Non-numeric characters will be ignored)");
- // Removes any non-numeric values.
- string2 = string2.replaceAll("[^0-9.]", "");
- // So long as an integer between 3 and 20 is entered, the cellBoard will have the number of columns entered.
- // Otherwise, the number of columns is set to 10.
- if (string2.equals("") == true || Integer.parseInt(string2) < 3 || Integer.parseInt(string2) > 20){
- JOptionPane.showMessageDialog (
- null, "Please enter an integer between 3 and 20 for the number of columns.\nColumns set to 10.",
- "Number Error",
- JOptionPane.ERROR_MESSAGE);
- cellBoardColumn = 10;
- }else {
- cellBoardColumn = Integer.parseInt(string2);
- }
- cellBoard = new Cell[cellBoardRow][cellBoardColumn];
- for (int row = 0; row < cellBoardRow; row++){
- for(int col = 0; col < cellBoardColumn; col++){
- cellBoard[row][col] = new Cell(null, null);
- }
- }
- // Asks the user for the number of players that will play.
- String string3 = JOptionPane.showInputDialog("How many players?:"
- + "\n(NOTE: Non-numeric characters will be ignored)");
- // Removes any non-numeric values.
- string3 = string3.replaceAll("[^0-9.]", "");
- // So long as the user entered an integer above 2, the game will set the entered value to the number of players.
- // Otherwise, the game is set to play with two players.
- if (string3.equals("") == true || Integer.parseInt(string3) < 2 || Integer.parseInt(string3) > 10){
- JOptionPane.showMessageDialog (
- null, "Please enter an integer between 2 and 10 for the number of players.\nPlayers set to 2.",
- "Number Error",
- JOptionPane.ERROR_MESSAGE);
- players = 2;
- }else {
- players = Integer.parseInt(string3);
- }
- // Asks the user which player will begin the match.
- String string4 = JOptionPane.showInputDialog("Which player will begin the match?:"
- + "\n(NOTE: Non-numeric characters will be ignored)");
- // Removes any non-numeric values.
- string4 = string4.replaceAll("[^0-9.]", "");
- // So long as the user entered an int above zero and less than or equal to the set number of players, the
- // game will have the player who was selected go first. Otherwise, player #1 goes first.
- if (string4.equals("") == true || Integer.parseInt(string4) <= 0 || Integer.parseInt(string4) > players){
- JOptionPane.showMessageDialog (
- null, "Please enter a valid player number for who will begin.\nPlayer 1 will begin.",
- "Number Error",
- JOptionPane.ERROR_MESSAGE);
- currentPlayer = 1;
- startingPlayer = 1;
- }else {
- currentPlayer = Integer.parseInt(string4);
- startingPlayer = Integer.parseInt(string4);
- }
- setNames();
- setMessage();
- }
- public void setNames(){
- int nameDialog1 = JOptionPane.showConfirmDialog(null, "Will Player 1 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog1 == JOptionPane.YES_OPTION){
- String nameInput1 = JOptionPane.showInputDialog("Player 1: what is your name?");
- if (nameInput1.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput1 = "Player #1";
- name1 = nameInput1;
- }else {
- name1 = nameInput1;
- }
- }else {
- name1 = "Player #1";
- }
- int nameDialog2 = JOptionPane.showConfirmDialog(null, "Will Player 2 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog2 == JOptionPane.YES_OPTION){
- String nameInput2 = JOptionPane.showInputDialog("Player 2: what is your name?");
- if (nameInput2.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput2 = "Player #2";
- name2 = nameInput2;
- }else {
- name2 = nameInput2;
- }
- }else {
- name2 = "Player #2";
- }
- if (players >= 3){
- int nameDialog3 = JOptionPane.showConfirmDialog(null, "Will Player 3 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog3 == JOptionPane.YES_OPTION){
- String nameInput3 = JOptionPane.showInputDialog("Player 3: what is your name?");
- if (nameInput3.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput3 = "Player #3";
- name3 = nameInput3;
- }else {
- name3 = nameInput3;
- }
- }else {
- name3 = "Player #3";
- }
- }
- if (players >= 4){
- int nameDialog4 = JOptionPane.showConfirmDialog(null, "Will Player 4 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog4 == JOptionPane.YES_OPTION){
- String nameInput4 = JOptionPane.showInputDialog("Player 4: what is your name?");
- if (nameInput4.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput4 = "Player #4";
- name4 = nameInput4;
- }else {
- name4 = nameInput4;
- }
- }else {
- name4 = "Player #4";
- }
- }
- if (players >= 5){
- int nameDialog5 = JOptionPane.showConfirmDialog(null, "Will Player 5 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog5 == JOptionPane.YES_OPTION){
- String nameInput5 = JOptionPane.showInputDialog("Player 5: what is your name?");
- if (nameInput5.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput5 = "Player #5";
- name5 = nameInput5;
- }else {
- name5 = nameInput5;
- }
- }else {
- name5 = "Player #5";
- }
- }
- if (players >= 6){
- int nameDialog6 = JOptionPane.showConfirmDialog(null, "Will Player 6 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog6 == JOptionPane.YES_OPTION){
- String nameInput6 = JOptionPane.showInputDialog("Player 6: what is your name?");
- if (nameInput6.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput6 = "Player #6";
- name6 = nameInput6;
- }else {
- name6 = nameInput6;
- }
- }else {
- name6 = "Player #6";
- }
- }
- if (players >= 7){
- int nameDialog7 = JOptionPane.showConfirmDialog(null, "Will Player 7 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog7 == JOptionPane.YES_OPTION){
- String nameInput7 = JOptionPane.showInputDialog("Player 7: what is your name?");
- if (nameInput7.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput7 = "Player #7";
- name7 = nameInput7;
- }else {
- name7 = nameInput7;
- }
- }else {
- name7 = "Player #7";
- }
- }
- if (players >= 8){
- int nameDialog8 = JOptionPane.showConfirmDialog(null, "Will Player 8 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog8 == JOptionPane.YES_OPTION){
- String nameInput8 = JOptionPane.showInputDialog("Player 8: what is your name?");
- if (nameInput8.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput8 = "Player #8";
- name8 = nameInput8;
- }else {
- name8 = nameInput8;
- }
- }else {
- name8 = "Player #8";
- }
- }
- if (players >= 9){
- int nameDialog9 = JOptionPane.showConfirmDialog(null, "Will Player 9 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog9 == JOptionPane.YES_OPTION){
- String nameInput9 = JOptionPane.showInputDialog("Player 9: what is your name?");
- if (nameInput9.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput9 = "Player #9";
- name9 = nameInput9;
- }else {
- name9 = nameInput9;
- }
- }else {
- name9 = "Player #9";
- }
- }
- if (players >= 10){
- int nameDialog10 = JOptionPane.showConfirmDialog(null, "Will Player 10 create a "
- + "custom name?","Name Select", JOptionPane.YES_NO_OPTION);
- if (nameDialog10 == JOptionPane.YES_OPTION){
- String nameInput10 = JOptionPane.showInputDialog("Player 10: what is your name?");
- if (nameInput10.equals("") == true){
- JOptionPane.showMessageDialog (
- null, "Nothing entered. Player name set to player number.",
- "Name Error",
- JOptionPane.ERROR_MESSAGE);
- nameInput10 = "Player #10";
- name10 = nameInput10;
- }else {
- name10 = nameInput10;
- }
- }else {
- name10 = "Player #10";
- }
- }
- }
- public int getRows(){
- return cellBoardRow;
- }
- public int getCol(){
- return cellBoardColumn;
- }
- public int getPlayers(){
- return players;
- }
- public int currentPlayer(){
- return currentPlayer;
- }
- public String getCurrentPlayerName(){
- if (currentPlayer == 1){
- return name1;
- }else if (currentPlayer == 2){
- return name2;
- }else if (currentPlayer == 3){
- return name3;
- }else if (currentPlayer == 4){
- return name4;
- }else if (currentPlayer == 5){
- return name5;
- }else if (currentPlayer == 6){
- return name6;
- }else if (currentPlayer == 7){
- return name7;
- }else if (currentPlayer == 8){
- return name8;
- }else if (currentPlayer == 9){
- return name9;
- }else{
- return name10;
- }
- }
- public int getStartPlayer(){
- return startingPlayer;
- }
- public int setStartPlayer(int plyr){
- startingPlayer = plyr;
- return startingPlayer;
- }
- public String getMessage(){
- return status;
- }
- public String setMessage(){
- status = "Victories: " + name1 + ": " + player1Score + " " + name2 + ": " + player2Score + " ";
- if (players >= 3){
- status = status + name3 + ": " + player3Score + " ";
- }
- if (players >= 4){
- status = status + name4 + ": " + player4Score + " ";
- }
- if (players >= 5){
- status = status + name5 + ": " + player5Score + " ";
- }
- if (players >= 6){
- status = status + name6 + ": " + player6Score + " ";
- }
- if (players >= 7){
- status = status + name7 + ": " + player7Score + " ";
- }
- if (players >= 8){
- status = status + name8 + ": " + player8Score + " ";
- }
- if (players >= 9){
- status = status + name9 + ": " + player9Score + " ";
- }
- if (players >= 10){
- status = status + name10 + ": " + player10Score + " ";
- }
- return status;
- }
- public int addToScore(int winner){
- if (winner == 1){
- player1Score++;
- }
- if (winner == 2){
- player2Score++;
- }
- if (winner == 3){
- player3Score++;
- }
- if (winner == 4){
- player4Score++;
- }
- if (winner == 5){
- player5Score++;
- }
- if (winner == 6){
- player6Score++;
- }
- if (winner == 7){
- player7Score++;
- }
- if (winner == 8){
- player8Score++;
- }
- if (winner == 9){
- player9Score++;
- }
- if (winner == 10){
- player10Score++;
- }
- return winner;
- }
- public Cell getCell(int row, int col){
- Cell c = cellBoard[row][col];
- return c;
- }
- public void nextPlayer(){
- if (currentPlayer == players){
- currentPlayer = 1;
- }else {
- currentPlayer++;
- }
- }
- public int setPlayer(int plyr){
- currentPlayer = plyr;
- return currentPlayer;
- }
- public Boolean select (int row, int col){
- Integer buttonNum = cellBoard[row][col].getPlayerNumber();
- if (buttonNum != null){
- return false;
- }else {
- return true;
- }
- }
- public Boolean catsCheck(){
- for (int row=0; row< getRows(); row++){ //beginning of first chunk of code given to us
- for(int col=0; col< getCol(); col++){
- if (cellBoard[row][col].getPlayerNumber() == null){
- return false;
- }
- }
- }
- return true;
- }
- public void reset() {
- for(int row = 0; row < getRows(); row++){
- for(int col= 0; col < getCol(); col++){
- cellBoard[row][col] = new Cell(null, null);
- }
- }
- }
- public int isWinner() {
- int endResult = -1;
- int rowConstant = (getRows() - 1);
- int colConstant = (getCol() - 1);
- if (catsCheck() == true){
- endResult = 0;
- return endResult;
- }
- for (int row=0; row< getRows(); row++){ //beginning of first chunk of code given to us
- for(int col=0; col< getCol(); col++){
- if (row == 0 && col == 0){ // Top-Left corner case (check 2 sides).
- if (cellBoard[row][col+1].getPlayerNumber() == cellBoard[row+1][col].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col+1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row+1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row][col+1].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }else if (row == 0 && col == colConstant){ // Top-Right corner case (check 2 sides).
- if (cellBoard[row][col-1].getPlayerNumber() == cellBoard[row+1][col].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col-1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row+1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row][col-1].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }else if (row == rowConstant && col == 0){ // Bottom-Left corner case (check 2 sides).
- if (cellBoard[row-1][col].getPlayerNumber() == cellBoard[row][col+1].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col+1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row-1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row-1][col].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }else if (row == rowConstant && col == colConstant){ // Bottom-Right corner case (check 2 sides).
- if (cellBoard[row-1][col].getPlayerNumber() == cellBoard[row][col-1].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col-1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row-1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row-1][col].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }else if (row == 0 && col != colConstant && col != 0){ // Top wall case (check 3 sides).
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row][col-1].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row][col+1].getPlayerNumber()){
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col+1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row+1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row-1][col].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }
- }else if (row == rowConstant && col != colConstant && col != 0){ // Bottom wall case (check 3 sides).
- if (cellBoard[row-1][col].getPlayerNumber() == cellBoard[row][col+1].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row-1][col].getPlayerNumber() == cellBoard[row][col-1].getPlayerNumber()){
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col+1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row-1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row+1][col].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }
- }else if (col == colConstant && row != rowConstant && row != 0){ // Right wall case (check 3 sides).
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row-1][col].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row][col-1].getPlayerNumber()){
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col-1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row-1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row][col-1].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }
- }else if (col == 0 && row != rowConstant && row != 0){ // Left wall case (check 3 sides).
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row-1][col].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row][col+1].getPlayerNumber()){
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col+1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row-1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row][col+1].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }
- }else if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row-1][col].getPlayerNumber()){ // Checks to see if numbers are the same.
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row][col+1].getPlayerNumber()){
- if (cellBoard[row+1][col].getPlayerNumber() == cellBoard[row][col-1].getPlayerNumber()){
- if (cellBoard[row][col].getPlayerNumber() != cellBoard[row][col+1].getPlayerNumber()){ // Makes sure you are not surrounding yourself.
- if (cellBoard[row-1][col].getPlayerNumber() != null){ // Makes sure the squares next to you aren't null.
- endResult = cellBoard[row][col+1].getPlayerNumber(); // Get the winner player.
- }
- }
- }
- }
- }
- }
- }
- return endResult;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement