Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Assignment 2
- *
- * @author (omer)
- * @version (12.5.17)
- */
- public class Assignment2 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int BOARD_WIDTH = 7; // == board[0].length
- int BOARD_HIEGHT = 6;// board.length
- int MAX_BOARDAY_SIZE = 30;
- int MAX_INT = 2147483647;
- int kelet;
- do{
- System.out.println("0. Exit");
- System.out.println("1. Four in a line");
- System.out.println("2. Remove Duplicates");
- System.out.println("3. 3-Closest Sum");
- System.out.print("Choose An Option (0-3): ");
- kelet = sc.nextInt();
- if(kelet == 0)
- {
- System.out.println("GoodBye");
- break;
- }
- if(kelet == 1)
- {
- /*
- * 1. all cells 0
- * while: main of the game:
- * 1. check if all board is full get out with who win
- * 2. check for legal input
- * 3. put in the board
- * 4. check winner by rows
- */
- int[][] board = new int[BOARD_HIEGHT][BOARD_WIDTH];
- //all cells 0 in array
- for(int i=0;i<board.length;i++)
- {
- for(int j=0;j<board[i].length;j++)
- {
- board[i][j]=0;
- }
- }
- //printing first time empty board
- System.out.println("Board :");
- for(int i = 0; i < BOARD_HIEGHT;i++)
- {
- for(int j = 0; j < BOARD_WIDTH;j++)
- {
- if(board[i][j] == 0)
- {
- System.out.print("| ");
- }
- else if(board[i][j] == 1)
- {
- System.out.print("|O");
- }
- else if(board[i][j] == 2)
- {
- System.out.print("|X");
- }
- }
- System.out.println("|");
- }
- for(int i = 0; i < 2*BOARD_WIDTH;i++)
- {
- System.out.print("-");
- }
- System.out.println("\n");
- int player = 1; //first or second player
- boolean gameRun = true; //when game over its change to false
- boolean firstWin = false;
- boolean secondWin = false;
- boolean isFull = true;
- while(gameRun == true)
- {
- isFull = true;
- for(int i=0;i<BOARD_WIDTH;i++)
- {
- if(board[0][i] == 0)
- isFull = false;
- }
- if(isFull == true)
- {
- System.out.println("Game finished with a draw\n");
- break;
- }
- //someone win before empty
- if(firstWin == true)
- {
- System.out.println("Player 1 won\n");
- break;
- }
- if(secondWin == true)
- {
- System.out.println("Player 2 won\n");
- break;
- }
- //if board isnt full the game keep running
- System.out.print("Player "+player+", choose a column: ");
- int putCol=0; //where the player put disc.
- boolean goodPlace = false;
- while(goodPlace == false) //pasul
- {
- putCol = sc.nextInt();
- if((putCol > BOARD_WIDTH)||(putCol < 1)) //out of array
- {
- System.out.println("Error: invalid column number "+putCol+"\n");
- }
- else
- {
- if(board[0][putCol-1]!=0) //fully column
- System.out.println("Error: column "+putCol+" is full\n");
- else
- {
- goodPlace = true;
- break;
- }
- }
- System.out.println("Board :");
- for(int i = 0; i < BOARD_HIEGHT;i++)
- {
- for(int j = 0; j < BOARD_WIDTH;j++)
- {
- if(board[i][j] == 0)
- {
- System.out.print("| ");
- }
- else if(board[i][j] == 1)
- {
- System.out.print("|O");
- }
- else if(board[i][j] == 2)
- {
- System.out.print("|X");
- }
- }
- System.out.println("|");
- }
- for(int i = 0; i < 2*BOARD_WIDTH;i++)
- {
- System.out.print("-");
- }
- System.out.println("\n");
- System.out.print("Player "+player+", choose a column: ");
- } //end pasul
- int placeToPut = BOARD_HIEGHT-1;
- while((board[placeToPut][putCol-1]!=0)&&(placeToPut>0))
- {
- placeToPut--;
- }
- //placing 1 or 2 in the needed place.......... putCol-1 cause player choose from 1 and array starts from 0
- board[placeToPut][putCol-1] = player;
- //printing board after adding
- System.out.println("Board :");
- for(int i = 0; i < BOARD_HIEGHT;i++)
- {
- for(int j = 0; j < BOARD_WIDTH;j++)
- {
- if(board[i][j] == 0)
- {
- System.out.print("| ");
- }
- else if(board[i][j] == 1)
- {
- System.out.print("|O");
- }
- else if(board[i][j] == 2)
- {
- System.out.print("|X");
- }
- }
- System.out.println("|");
- }
- for(int i = 0; i < 2*BOARD_WIDTH;i++)
- {
- System.out.print("-");
- }
- System.out.println("\n");
- //check if someone wins
- for(int i=0; i<board.length;i++)
- {
- for(int j=0;j<board[i].length; j++)
- {
- //win by rows
- if(j+3<board[i].length)
- {
- if((board[i][j]==board[i][j+1])&&(board[i][j+2]==board[i][j+3])&&(board[i][j]==board[i][j+3])&&(board[i][j]==1))
- {
- firstWin = true;
- }
- if((board[i][j]==board[i][j+1])&&(board[i][j+2]==board[i][j+3])&&(board[i][j]==board[i][j+3])&&(board[i][j]==2))
- {
- secondWin = true;
- }
- }
- //win by columns
- if(i+3<board.length)
- {
- if((board[i][j]==board[i+1][j])&&(board[i+2][j]==board[i+3][j])&&(board[i][j]==board[i+3][j])&&(board[i][j]==1))
- {
- firstWin = true;
- }
- if((board[i][j]==board[i+1][j])&&(board[i+2][j]==board[i+3][j])&&(board[i][j]==board[i+3][j])&&(board[i][j]==2))
- {
- secondWin = true;
- }
- }
- //win by diagonal alhson \
- if((i+3<board.length)&&(j+3<board[i].length))
- {
- if((board[i][j]==board[i+1][j+1])&&(board[i+2][j+2]==board[i+3][j+3])&&(board[i][j]==board[i+3][j+3])&&(board[i][j]==1))
- {
- firstWin = true;
- }
- if((board[i][j]==board[i+1][j+1])&&(board[i+2][j+2]==board[i+3][j+3])&&(board[i][j]==board[i+3][j+3])&&(board[i][j]==2))
- {
- secondWin = true;
- }
- }
- //win by diagonal alhson /
- if((i+3<board.length)&&(j-3>=0))
- {
- if((board[i][j]==board[i+1][j-1])&&(board[i+2][j-2]==board[i+3][j-3])&&(board[i][j]==board[i+3][j-3])&&(board[i][j]==1))
- {
- firstWin = true;
- }
- if((board[i][j]==board[i+1][j-1])&&(board[i+2][j-2]==board[i+3][j-3])&&(board[i][j]==board[i+3][j-3])&&(board[i][j]==2))
- {
- secondWin = true;
- }
- }
- }
- }
- if(player == 1)
- player = 2;
- else if(player == 2)
- player = 1;
- }//end gameRun
- }//end if(kelet==1)
- if(kelet == 2)
- {
- System.out.println("");
- System.out.print("Enter array size: ");
- int size = sc.nextInt();
- boolean goodSize = true; //will use it, so if it false it will back to menu as needed
- if(size > MAX_BOARDAY_SIZE)
- {
- System.out.println("Error: array's size must be at most " +MAX_BOARDAY_SIZE);
- goodSize = false;
- }
- else
- {
- if(size < 1)
- {
- System.out.println("Error: invalid array size " +size);
- goodSize = false;
- }
- }
- if(goodSize)
- {
- System.out.println("Enter the numbers:");
- int[] secMis = new int[size];
- boolean found;
- for(int i = 0; i < size; i++)
- {
- secMis[i] = sc.nextInt();
- }
- int[] saveNum = new int[size];
- /*
- * i create one more BOARDay, the biggest size is "size" cause
- * could be most of all numbers are diffent.
- */
- for(int i = 0;i <size;i++)
- {
- saveNum[i] = 99999999; //saving this num as temp to stop print. Using as "zakif"
- }
- int placeInsert=0;
- for(int i = 0;i <size;i++)
- {
- found = false;
- for(int j = 0;j <size;j++)
- {
- if(secMis[i] == saveNum[j])
- {
- found = true;
- }
- }
- if(found == false) //the number that appear in the secMis BOARDay which save inputs doesnt appear in the output BOARDay so we add it
- {
- saveNum[placeInsert] = secMis[i];
- placeInsert++;
- }
- }
- //printing BOARDay after remuving duplications, printing till 99999999 "zakif"
- System.out.println("Solution: ");
- System.out.print("{" +saveNum[0]); //we have at least one number
- for(int i=1;i<size;i++)
- {
- if(saveNum[i] != 99999999)
- {
- System.out.print(", " +saveNum[i]);
- }
- }
- System.out.println("}\n");
- }
- } //mission 2
- if(kelet == 3)
- {
- System.out.print("\nEnter array size: ");
- int size = sc.nextInt();
- boolean goodSize = true;
- if(size > MAX_BOARDAY_SIZE)
- {
- System.out.println("Error: array's size must be at most" +MAX_BOARDAY_SIZE);
- goodSize = false;
- }
- if(size < 3)
- {
- System.out.println("Error: array's size must be at least 3");
- goodSize = false;
- }
- if(goodSize == true)
- {
- int ABCHefresh = MAX_INT; //the hefresh will bw always smaller then max int so if you will coose huge numbers in array and small target sum it still works
- int A = MAX_INT;
- int B = MAX_INT;
- int C = MAX_INT;
- int sumABC = 0;
- //save the three nums that there sum close to input sum as A B C
- //hefresh saves the sub between the target and the 3 indexes sum (but with abs as if)
- //if the tempHefresh which saves tha I J K indexes smaller than the ABCHefresh we swap between them
- System.out.println("Enter the numbers:");
- int[] thiMis = new int[size]; //BOARDay for third mission
- for(int i=0; i <size;i++)
- {
- thiMis[i] = sc.nextInt();
- }
- System.out.print("Enter the Target Sum: ");
- int target = sc.nextInt();
- for(int i=0; i<size-2; i++)
- {
- for(int j=i+1;j<size-1; j++)
- {
- for(int k=j+1; k<size; k++)
- {
- int tempSum = thiMis[i] + thiMis[j] + thiMis[k];
- int tempHefresh = target-tempSum; //pow and sqrt to find the abs of the num
- //abs
- if(tempHefresh < 0)
- {
- tempHefresh = tempHefresh*(-1);
- }
- if(tempHefresh < ABCHefresh) //found smaller hafresh so we save the numbers at A B C
- {
- A = thiMis[i];
- B = thiMis[j];
- C = thiMis[k];
- ABCHefresh = tempHefresh;
- }
- }
- }
- }
- System.out.println("Solution: ");
- sumABC = A + B + C;
- System.out.println("{" +A+ ", " +B+ ", " +C+ "}, Closest Sum: " +sumABC);
- }
- }
- if((kelet != 0)&&(kelet != 1)&&(kelet != 2)&&(kelet !=3))
- {
- System.out.println("Error: invalid menu option " +kelet);
- }
- }while(kelet != 0);
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement