Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I loop my minigame inside my game called PetCare

+4 votes
asked Jan 27, 2021 by THOMAS GUERRA (280 points) 1 flag
Please check my shareCode.


Please let me know what should I do in depth please. I have to finish this by Friday.
please also add steps so its more understandable. Thank you so much!

1 Answer

0 votes
answered Jan 28, 2021 by Peter Minarik (88,520 points)
edited Jan 28, 2021 by Peter Minarik

I had a quick look on your code. It compiles and seems to work to a good extent, though looks incomplete and have some bugs here and there.

Do you have any specific questions?

A few things I noticed:

Problem #1

In TTT.java: private boolean check???ForWin() methods (all of them):

private boolean checkRowsForWin()
    for(int i = 0; i<3; i++)
        if(checkRowCol(board[i][0], board[i][1], board[i][2])==true); // <<<< Notice the semicolon here
            return true;
    return false;

Highlighted with red background there is a semicolon after the if statement in all of your win checker functions. This terminates the if with an empty statement and the next statement below (return true;) is always executed as it is not conditional anymore. Hence after every step, the minigame thinks you won.

Solution: remove the semicolon.

Problem #2

In Animal.java: public void getAllStats()
The name suggests that you get some kind of information (only read the Animal class), but the code actually changes the state of the Animal. I think this is misleading naming. Maybe call it ChangeStats or ElapseTime.

Problem #3

Also, I think I'd make Petstats be encapsulated within Pet. I think this is a 'has-a' relationship, so they should be tightly coupled. After this change, there's no need to have a name field in the Petstats class.

Problem #4

Your indentation is also sometimes inconsistent. I'd suggest when you enter a scope (within { and }) you indent the content inside and not have the same indentation as the opening and closing braces (but one extra level).

Problem #5

Also, I think the name and the type of the animal could be marked final, as they would never change. Right?

Problem #6

In TTT.Java: private boolean checkDiagonalsForWin()
You loop 3 times (unnecessary) and only check one diagonal, but no the other.
I think the correct code would be:

private boolean checkDiagonalsForWin()
    return checkRowCol(board[0][0], board[1][1], board[2][2]) ||
           checkRowCol(board[2][0], board[1][1], board[0][2]);
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.