repl.it
@JamesFyfe/

Scrabble Winner

Java

No description

fork
loading
Files
  • Main.java
  • SCORES.txt
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.File;
import java.util.Scanner;
import java.io.IOException;

class Main 
{
  public static void main(String[] args) throws Exception   //throws file not found exception
  {
    int[] total = {0, 0, 0, 0};
    int pts, max, hold, numPlayers = total.length;
    String [] player = {"James", "Sam", "Zach", "Andy"};
    String line;
    
    File file = new File("SCORES.txt");
    
    Scanner sc = new Scanner(System.in);
    sc = new Scanner(file);

      while (sc.hasNextLine())
      {
        line = sc.nextLine();

        if(line.charAt(7) == 'J')  //character 7 is first letter of players name
        {
          pts = getNumber(line); //finds how many points earned this turn
          total[0] += pts; //adds points to total
        }

        else if(line.charAt(7) == 'S')
        {
          pts = getNumber(line);
          total[1] += pts;
        }

        else if(line.charAt(7) == 'Z')
        {
          pts = getNumber(line);
          total[2] += pts;
        }
        
        else if(line.charAt(7) == 'A')
        {
          pts = getNumber(line);
          total[3] += pts;
        }

      }

          for (int i = 0; i < numPlayers -1; i++) //Sorts total scores greatest to least and swaps the names the same way to stay matched
          {
            max = i;
            for (int j = i + 1; j < numPlayers; j++)
            {
              if (total[j] > total[max]) //if a number is higher than max
              {
                max = j; //that number is now max
              }
            }
            swapValues(total, i, max); //swaps new max with old max
            swapPlayers(player, i, max); //swaps player name
          }
          
      for(int i = 0; i < numPlayers; i++)
        if(i == 0)
          System.out.println("Congratulations " + player[i] + "! you won with " + total[i] + " points!");

        else if(i == numPlayers - 1)
          System.out.println("Sorry " + player[i] + ", you lost with " + total[i] + " points");

        else
          System.out.println(player[i] + " got " + total[i] + " points");
  }


  static int getNumber(String line) //singles out the points earned on a given turn
  {
    String[] lineArr = line.split("\\(|\\spts");//splits line into a 3 element array at at ( and \\spts)
    int pts = Integer.parseInt(lineArr[1]);//turns this element which is just the points from a string into an integer

    return pts;
  }


  static void swapValues (int[] total, int i, int max) //swaps 2 numbers
  {
    int hold = total[max];
    total[max] = total[i];
    total[i] = hold;
  }


  static void swapPlayers (String[] player, int i, int max) //swaps 2 players
  {
    String hold = player[max];
    player[max] = player[i];
    player[i] = hold;
  }
}