Advertisement
OmerChovel

mission 2

Apr 28th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.65 KB | None | 0 0
  1. import java.util.Scanner;
  2. /**
  3.  * Assignment 2
  4.  *
  5.  * @author (omer)
  6.  * @version (12.5.17)
  7.  */
  8.  
  9. public class Assignment2 {
  10.     public static void main(String[] args) {
  11.         Scanner sc  = new Scanner(System.in);
  12.         int BOARD_WIDTH = 7; // == board[0].length
  13.         int BOARD_HIEGHT = 6;// board.length
  14.         int MAX_BOARDAY_SIZE = 30;
  15.         int MAX_INT = 2147483647;
  16.         int kelet;
  17.         do{
  18.             System.out.println("0. Exit");
  19.             System.out.println("1. Four in a line");
  20.             System.out.println("2. Remove Duplicates");
  21.             System.out.println("3. 3-Closest Sum");
  22.             System.out.print("Choose An Option (0-3): ");            
  23.             kelet = sc.nextInt();
  24.            
  25.             if(kelet == 0)
  26.             {
  27.                 System.out.println("GoodBye");
  28.                 break;
  29.             }
  30.            
  31.             if(kelet == 1)
  32.             {
  33.                 /*
  34.                  * 1. all cells 0
  35.                  * while: main of the game:
  36.                  * 1. check if all board is full get out with who win
  37.                  * 2. check for legal input
  38.                  * 3. put in the board
  39.                  * 4. check winner by rows
  40.                  */
  41.                
  42.                 int[][] board = new int[BOARD_HIEGHT][BOARD_WIDTH];
  43.                 //all cells 0 in array
  44.                 for(int i=0;i<board.length;i++)
  45.                 {
  46.                     for(int j=0;j<board[i].length;j++)
  47.                     {
  48.                         board[i][j]=0;
  49.                     }
  50.                 }
  51.                
  52.                 //printing first time empty board
  53.                 System.out.println("Board :");
  54.                 for(int i = 0; i < BOARD_HIEGHT;i++)
  55.                 {
  56.                     for(int j = 0; j < BOARD_WIDTH;j++)
  57.                     {
  58.                         if(board[i][j] == 0)
  59.                         {
  60.                             System.out.print("| ");
  61.                         }
  62.                         else if(board[i][j] == 1)
  63.                         {
  64.                             System.out.print("|O");
  65.                         }
  66.                         else if(board[i][j] == 2)
  67.                         {
  68.                             System.out.print("|X");
  69.                         }
  70.                     }
  71.                     System.out.println("|");
  72.                 }
  73.                 for(int i = 0; i < 2*BOARD_WIDTH;i++)
  74.                 {
  75.                     System.out.print("-");
  76.                 }
  77.                 System.out.println("\n");
  78.                  
  79.                
  80.                
  81.                 int player = 1; //first or second player
  82.                 boolean gameRun = true; //when game over its change to false
  83.                 boolean firstWin = false;
  84.                 boolean secondWin = false;
  85.                 boolean isFull = true;
  86.                 while(gameRun == true)
  87.                 {
  88.                     isFull = true;
  89.                     for(int i=0;i<BOARD_WIDTH;i++)
  90.                     {
  91.                         if(board[0][i] == 0)
  92.                             isFull = false;
  93.                     }
  94.                    
  95.                     if(isFull == true)
  96.                     {
  97.                         System.out.println("Game finished with a draw\n");
  98.                         break;
  99.                     }
  100.                    
  101.                     //someone win before empty
  102.                     if(firstWin == true)
  103.                     {
  104.                         System.out.println("Player 1 won\n");
  105.                         break;
  106.                     }
  107.                     if(secondWin == true)
  108.                     {
  109.                         System.out.println("Player 2 won\n");
  110.                         break;
  111.                     }
  112.                    
  113.                     //if board isnt full the game keep running
  114.                     System.out.print("Player "+player+", choose a column: ");
  115.                     int putCol=0; //where the player put disc.
  116.                     boolean goodPlace = false;
  117.                     while(goodPlace == false) //pasul
  118.                     {
  119.                         putCol = sc.nextInt();
  120.                         if((putCol > BOARD_WIDTH)||(putCol < 1)) //out of array
  121.                         {
  122.                             System.out.println("Error: invalid column number "+putCol+"\n");
  123.                         }
  124.                         else
  125.                         {
  126.                             if(board[0][putCol-1]!=0) //fully column
  127.                                 System.out.println("Error: column "+putCol+" is full\n");
  128.                             else
  129.                             {
  130.                                 goodPlace = true;
  131.                                 break;
  132.                             }
  133.                         }
  134.                      
  135.                        
  136.                         System.out.println("Board :");
  137.                         for(int i = 0; i < BOARD_HIEGHT;i++)
  138.                         {
  139.                             for(int j = 0; j < BOARD_WIDTH;j++)
  140.                             {
  141.                                 if(board[i][j] == 0)
  142.                                 {
  143.                                     System.out.print("| ");
  144.                                 }
  145.                                 else if(board[i][j] == 1)
  146.                                 {
  147.                                     System.out.print("|O");
  148.                                 }
  149.                                 else if(board[i][j] == 2)
  150.                                 {
  151.                                     System.out.print("|X");
  152.                                 }
  153.                             }
  154.                             System.out.println("|");
  155.                         }
  156.                         for(int i = 0; i < 2*BOARD_WIDTH;i++)
  157.                         {
  158.                             System.out.print("-");
  159.                         }
  160.                         System.out.println("\n");
  161.                        
  162.                         System.out.print("Player "+player+", choose a column: ");
  163.                     } //end pasul
  164.                    
  165.                     int placeToPut = BOARD_HIEGHT-1;
  166.                     while((board[placeToPut][putCol-1]!=0)&&(placeToPut>0))
  167.                     {
  168.                         placeToPut--;
  169.                     }
  170.                      
  171.                     //placing 1 or 2 in the needed place.......... putCol-1 cause player choose from 1 and array starts from 0
  172.                     board[placeToPut][putCol-1] = player;
  173.                    
  174.                     //printing board after adding
  175.                     System.out.println("Board :");
  176.                     for(int i = 0; i < BOARD_HIEGHT;i++)
  177.                     {
  178.                          for(int j = 0; j < BOARD_WIDTH;j++)
  179.                          {
  180.                             if(board[i][j] == 0)
  181.                             {
  182.                                 System.out.print("| ");
  183.                             }
  184.                             else if(board[i][j] == 1)
  185.                             {
  186.                                 System.out.print("|O");
  187.                             }
  188.                             else if(board[i][j] == 2)
  189.                             {
  190.                                 System.out.print("|X");
  191.                             }
  192.                          }
  193.                          System.out.println("|");
  194.                     }
  195.                     for(int i = 0; i < 2*BOARD_WIDTH;i++)
  196.                     {
  197.                          System.out.print("-");
  198.                     }
  199.                         System.out.println("\n");
  200.                     //check if someone wins
  201.                     for(int i=0; i<board.length;i++)
  202.                     {
  203.                         for(int j=0;j<board[i].length; j++)
  204.                         {
  205.                             //win by rows
  206.                             if(j+3<board[i].length)
  207.                             {
  208.                                 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))
  209.                                 {
  210.                                     firstWin = true;                                  
  211.                                 }
  212.                                 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))
  213.                                 {
  214.                                     secondWin = true;
  215.                                 }
  216.                             }
  217.                            
  218.                             //win by columns
  219.                             if(i+3<board.length)
  220.                             {
  221.                                 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))
  222.                                 {
  223.                                     firstWin = true;        
  224.                                 }
  225.                                 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))
  226.                                 {
  227.                                     secondWin = true;        
  228.                                 }
  229.                             }
  230.                            
  231.                             //win by diagonal alhson \
  232.                            
  233.                             if((i+3<board.length)&&(j+3<board[i].length))
  234.                             {
  235.                                 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))
  236.                                 {
  237.                                     firstWin = true;        
  238.                                 }
  239.                                 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))
  240.                                 {
  241.                                     secondWin = true;        
  242.                                 }
  243.                             }
  244.                            
  245.                             //win by diagonal alhson /
  246.                             if((i+3<board.length)&&(j-3>=0))
  247.                             {
  248.                                 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))
  249.                                 {
  250.                                     firstWin = true;        
  251.                                 }
  252.                                 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))
  253.                                 {
  254.                                     secondWin = true;        
  255.                                 }
  256.                             }
  257.                         }
  258.                     }
  259.                    
  260.                     if(player == 1)
  261.                         player = 2;
  262.                     else if(player == 2)
  263.                         player = 1;
  264.                 }//end gameRun
  265.                
  266.             }//end if(kelet==1)
  267.            
  268.             if(kelet == 2)
  269.             {
  270.                 System.out.println("");
  271.                 System.out.print("Enter array size: ");
  272.                 int size = sc.nextInt();
  273.                 boolean goodSize = true; //will use it, so if it false it will back to menu as needed
  274.                 if(size > MAX_BOARDAY_SIZE)
  275.                 {
  276.                     System.out.println("Error: array's size must be at most " +MAX_BOARDAY_SIZE);
  277.                     goodSize = false;
  278.                 }
  279.                 else
  280.                 {
  281.                     if(size < 1)
  282.                     {
  283.                         System.out.println("Error: invalid array size " +size);
  284.                         goodSize = false;
  285.                     }
  286.                 }  
  287.                
  288.                 if(goodSize)
  289.                 {
  290.                     System.out.println("Enter the numbers:");
  291.                     int[] secMis = new int[size];
  292.                     boolean found;
  293.                     for(int i = 0; i < size; i++)
  294.                     {
  295.                         secMis[i] = sc.nextInt();
  296.                     }
  297.                     int[] saveNum = new int[size];
  298.                     /*
  299.                      * i create one more BOARDay, the biggest size is "size" cause
  300.                      * could be most of all numbers are diffent.
  301.                      */
  302.                    
  303.                     for(int i = 0;i <size;i++)
  304.                     {
  305.                         saveNum[i] = 99999999; //saving this num as temp to stop print. Using as "zakif"
  306.                     }
  307.                    
  308.                     int placeInsert=0;
  309.                     for(int i = 0;i <size;i++)
  310.                     {
  311.                         found = false;
  312.                         for(int j = 0;j <size;j++)
  313.                         {
  314.                             if(secMis[i] == saveNum[j])
  315.                             {
  316.                                 found = true;
  317.                             }
  318.                         }
  319.                        
  320.                         if(found == false) //the number that appear in the secMis BOARDay which save inputs doesnt appear in the output BOARDay so we add it
  321.                         {
  322.                             saveNum[placeInsert] = secMis[i];
  323.                             placeInsert++;
  324.                         }
  325.                     }
  326.                    
  327.                     //printing BOARDay after remuving duplications, printing till 99999999 "zakif"
  328.                     System.out.println("Solution: ");
  329.                     System.out.print("{" +saveNum[0]); //we have at least one number
  330.                     for(int i=1;i<size;i++)
  331.                     {
  332.                         if(saveNum[i] != 99999999)
  333.                         {  
  334.                             System.out.print(", " +saveNum[i]);
  335.                         }
  336.                     }
  337.                     System.out.println("}\n");
  338.                 }
  339.             }   //mission 2
  340.            
  341.             if(kelet == 3)
  342.             {
  343.                 System.out.print("\nEnter array size: ");
  344.                 int size = sc.nextInt();
  345.                 boolean goodSize = true;
  346.                 if(size > MAX_BOARDAY_SIZE)
  347.                 {
  348.                     System.out.println("Error: array's size must be at most" +MAX_BOARDAY_SIZE);
  349.                     goodSize = false;
  350.                 }
  351.                 if(size < 3)
  352.                 {
  353.                     System.out.println("Error: array's size must be at least 3");
  354.                     goodSize = false;
  355.                 }
  356.                
  357.                 if(goodSize == true)
  358.                 {
  359.                     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
  360.                     int A = MAX_INT;
  361.                     int B = MAX_INT;
  362.                     int C = MAX_INT;
  363.                     int sumABC = 0;
  364.                     //save the three nums that there sum close to input sum as A B C
  365.                     //hefresh saves the sub between the target and the 3 indexes sum (but with abs as if)
  366.                     //if the tempHefresh which saves tha I J K indexes smaller than the ABCHefresh we swap between them
  367.                     System.out.println("Enter the numbers:");
  368.                     int[] thiMis = new int[size];   //BOARDay for third mission
  369.                     for(int i=0; i <size;i++)
  370.                     {
  371.                         thiMis[i] = sc.nextInt();
  372.                     }
  373.                    
  374.                     System.out.print("Enter the Target Sum: ");
  375.                     int target = sc.nextInt();
  376.                    
  377.                     for(int i=0; i<size-2; i++)
  378.                     {
  379.                         for(int j=i+1;j<size-1; j++)
  380.                         {
  381.                             for(int k=j+1; k<size; k++)
  382.                             {
  383.                                 int tempSum = thiMis[i] + thiMis[j] + thiMis[k];
  384.                                 int tempHefresh = target-tempSum; //pow and sqrt to find the abs of the num
  385.                                 //abs
  386.                                 if(tempHefresh < 0)
  387.                                 {  
  388.                                     tempHefresh = tempHefresh*(-1);
  389.                                 }
  390.                                                                                              
  391.                                 if(tempHefresh < ABCHefresh)    //found smaller hafresh so we save the numbers at A B C
  392.                                 {
  393.                                     A = thiMis[i];
  394.                                     B = thiMis[j];
  395.                                     C = thiMis[k];
  396.                                     ABCHefresh = tempHefresh;
  397.                                 }
  398.                             }
  399.                         }
  400.                     }
  401.                    
  402.                     System.out.println("Solution: ");
  403.                     sumABC = A + B + C;
  404.                     System.out.println("{" +A+ ", " +B+ ", " +C+ "}, Closest Sum: " +sumABC);
  405.                 }
  406.             }
  407.            
  408.             if((kelet != 0)&&(kelet != 1)&&(kelet != 2)&&(kelet !=3))
  409.             {
  410.                 System.out.println("Error: invalid menu option " +kelet);
  411.             }
  412.            
  413.         }while(kelet != 0);
  414.        
  415.         sc.close();
  416.     }
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement