Two Players Score Board (card game scoring logic .. implementing scoring rules)

I have a point counter for two players, when one reaches 11 points it is saved in TinyDB and displayed in ListView, so far ok, but if one of the players is 10 to 10 then counting should continue until one of the players has 2 points more then save.
what is the best way to do this?

Is this about table tennis?

is a card game

Is there any way to implement the logic

Yes. This is a suggestion for a strategy to make it happen:

First determine the required logic . Initially do that without using a TInyDB or immediately adding to a List of scoring history which add complications. Use a global variable for each player to save the player scores instead of storing immediately to a TinyDB. The coding logic will be easier to test this way.

Use the Logic and and or Blocks (see Programming Your App to Make Decisions and the section regarding complex situations). The code could be something like

If player1count and player2count = 10 then continue counting
if player1count = player2count + 2 then player 1 wins else if
player2count = player1 count +2 then player 2 wins

would be part of what you need to code. You need code to for 11 to 10 etc. too and make use of the or logic somewhere along the line,

When you get this code working then figure out how to save using the TInyDB and refactor the code to use local variables (if you feel that is really necessary)

There are various ways to implement your scoring rules. The above is one possibility.

That's exactly what I meant, how and where can I insert this logic as blocks? I have the first one when the player reaches 11 points then display it in the list view and save it

I wouldn't even attempt a guess Andrea. I do not totally understand the rules you hope to implement. I am suggesting you start a new version of your Project by first implementing the scoring logic if..then..else and or Blocks. Get the logic working then figure out how to display in the ListView.

Sorry, I can't fix your present app. :cry:

That isn't 'the first one' I suppose. The first one should trap 10 to 10 shouldn't it? Because that rule changes how you score and need to have 2 points more than the opponent to win.

I'm trying to implement this in Java, maybe you understand what I mean, as I said, I can't handle the blocks.

    private int player1Score = 0;
    private int player2Score = 0;

    public void player1Scores() {
        player1Score++;
    }

    public void player2Scores() {
        player2Score++;
    }

    public String getScore() {
        if (player1Score >= 10 && player2Score >= 10) {
            // Both players have at least 10 points
            if (Math.abs(player1Score - player2Score) >= 2) {
                // At least 2 points difference, one player wins
                return player1Score > player2Score ? "Player 1 wins" : "Player 2 wins";
            } else {
                // The game continues
                return "The game continues: " + player1Score + " - " + player2Score;
            }
        } else if (player1Score >= 11) {
            // Player 1 wins
            return "Player 1 wins";
        } else if (player2Score >= 11) {
            // Player 2 wins
            return "Player 2 wins";
        } else {
            // The game continues
            return player1Score + " - " + player2Score;
        }
    }

Yes, the Java logic can be replicated with App Inventor Blocks in a Procedure

for example

if (player1Score >= 10 && player2Score >= 10)

is if player1Score >= 10 and player2SCore >= 10 in Blocks

return is essentially equivalent to then in Blocks

It has been a while since I last programmed in Java. I'll think about how to refactor this but someone else might provide you an example before I do. :wink:

This Procedure and logic might work. I haven't tested it yet and no more time today.

Why not try the Procedure with dummy data and tell me if it works. :wink:

Oh my, it does indeed work. :smile:
scoringLogicRules.aia (3.3 KB)

scoring2

You can add ListView continuous play by play yourself; it is easy

3 Likes

I would like to thank you very much, works as expected, with all due respect, I wouldn't have thought of that.
Procedure = Function

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.