Jump to content

Math.max(x,y) comparing 4 integers (Java)

Claryn

Hi.

 

I made a very simple game in Java where there are 4 players, and they receive points for various tasks. Now I want to check who got the most points, and figured I could use the Math.max() method. However, it only works for 2 integers, and I am very unsure about how it actually works.

 

What I have done, is I have taken the score of every player and made into an integer.

        int a = Integer.parseInt(scoreP1.getText());        int b = Integer.parseInt(scoreP2.getText());        int c = Integer.parseInt(scoreP3.getText());        int d = Integer.parseInt(scoreP4.getText());

I figured I would first compare the three first, and then compare the last:

        int i = Math.max(a,Math.max(b,c));        int winner = Math.max(i,d);

But now int winner = the highest value, and not the int corresponding to each player. (a = p1 etc.)

I also don't know what to the method does if two values are the same. 

 

Any tips?

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe add something like this ?

String pwin;switch (winner){	case a:		pwin = "p1";		break;	case b:		pwin = "p2";		break;	case c:		pwin = "p3";		break;	case d:		pwin = "p4";		break;}

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to comment
Share on other sites

Link to post
Share on other sites

Are the scorePx variables integers? Why are you getting the text of the variable then parsing that into an integer? That's doing two conversions for no reason whatsoever.

 

You want an object that represents a player that has fields such as Player ID and Score, then you want an object that represents your players as a single entity so you can perform group operations on the players such as getting the highest score. In this object you'd have an array of Player objects and you loop through this array to find the highest number which is pretty trivial (if you can't figure it out then google find highest number in an array).

 

Edit: It occurred to me that the scorePx variables might be fields from a GUI in which case you're still doing it wrong, the GUI and game logic should be as separated as possible, look up the MVC (Model, View, Controller) pattern.

Link to comment
Share on other sites

Link to post
Share on other sites

Are the scorePx variables integers? Why are you getting the text of the variable then parsing that into an integer? That's doing two conversions for no reason whatsoever.

 

You want an object that represents a player that has fields such as Player ID and Score, then you want an object that represents your players as a single entity so you can perform group operations on the players such as getting the highest score. In this object you'd have an array of Player objects and you loop through this array to find the highest number which is pretty trivial (if you can't figure it out then google find highest number in an array).

 

Edit: It occurred to me that the scorePx variables might be fields from a GUI in which case you're still doing it wrong, the GUI and game logic should be as separated as possible, look up the MVC (Model, View, Controller) pattern.

 

ScorePx are jTextFields, so they are Strings. That is why I have to make them into ints.

 

Good idea. I can use a sorting method to find the largest integer in an array and then check if the highest one is the same as any of the others (a tie). 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

In C++ you could make a vector and make a custom sorting function, something like you have an array of players (class/struct) , and in the sorting function you'd check the score of those classes.

 

Or, you could just find the max score as you did then compare it to each player's score until you find it. If there's more than 1 player found with that score, then it's a tie.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Make a list consisting of (player, score) pairs. Then you can sort the list based on the score attribute.

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

This is an expanded version of the Math.max() method that I just made (I just quickly tested it and it seams to work) (Not sure if it will help but u may be able to adapt it)

 public static int max(int a, int b, int c, int d){	    return a >= b? (a >= c ? (a >= d ? a: d) : (c >= b ? (c >= d ? d: c) : (b >= d ? b : d))): (b >= c ? (b >= d? b : d) : (c >= a ? (c >= d ? c : d) : (a >= d ? a : d)));	  }
Link to comment
Share on other sites

Link to post
Share on other sites

I would add all of them to a list and use Collections.max(list)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×