Introduction java programming information



Download 1,4 Mb.
bet7/9
Sana30.04.2022
Hajmi1,4 Mb.
#595693
1   2   3   4   5   6   7   8   9
Bog'liq
kurs ishi 2

Generate Solution
Before we can start generating a game, we must first generate a solution. This is achieved by the method below, which needs to be called by the user as generate wordgame (new int[9][9], 0). The following steps are taken:

  1. Check if a solution is found.

    • Found -> solution is returned.

    • Not found -> continue.

  2. X (of current field) is found by finding the remainder of the division of the current index by the count of fields in a row using the modulo operation.

  3. Y (of current field) is found by dividing the current index by the count of fields in a row.

  4. An ArrayList is filled with the numbers 1 to 9 and shuffled. Shuffling is important because otherwise you always get the same solution.

  5. As long as there are numbers in the ArrayList, the following will be executed:

    1. The next possible number is obtained by the method getNextPossibleNumber(int[][], int, int, List), which will be explained later on. If there's no next possible number (return value of -1), null is returned.

    2. Found number is placed on the current location.

    3. The method is recursively called with an increase of the index, and the returning value is stored in a variable.

    4. If this variable is not null, it is returned; otherwise, the current location is put back to 0 (meaning the field is a blank).

  1. null is returned. This part will never be reached, hopefully.



Generate Game
Generating a game is simply achieved by constantly removing a random field and making sure the game is still valid. Valid means there is only one solution. This is achieved by the methods below. The user should call the first method, which uses the second method. I will describe the steps again.

  1. A list is filled with all possible positions.

  2. The list is shuffled. I do not know why. I suspect that this way, the blanks are better distributed. With the result that the game is harder.

The list is passed to the method generateGame(int[][], List) and the return value will be returned

  1. As long as there are positions in the list, the following will be executed:

    1. A position is taken from the list and stored in a variable.

    2. x and y are calculated from this position.

    3. Value at this position is stored in the variable temp.

    4. Value at this position is set to 0 (meaning the field is a blank).

    5. This step is critical. As the removal of the value at the position means that the game is no longer valid, the value at the position is put back. Otherwise the game stays the same.

  2. The game is returned.

A valid game has in every row, every column, and every region the numbers 1 to 9. Additionally, there should only be one solution existing. To achieve this, all open fields are filled with the first valid value. Even after finding a solution, the search continues by putting the next valid value in an open field. If a second solution is found, then the search will be stopped and the method returns false. There will always be at least one solution (hencegame is an incomplete solution), so if there are less than two solutions, the game is valid and the method returnstrue. Step by step:


  1. Check if a solution is found.

    • Found -> increase numberOfSolutions and return true if it equals 1; false otherwise.

    • Not found -> continue.

  2. Calculate x and y from index.

  3. Check if current field is a blank (equals 0).

    • True

      1. Fill a list with numbers 1 to 9.

      2. While the list contains numbers, execute the following:

        1. Get the next possible number. If the returned value equals -1, perform a break resulting a return of true.

        2. Set this number at current field.

        3. Call this method recursively and instantly check against the returned value.

          • True -> one or no solution found, continue search.

          • False -> more than one solution found, stop searching. Restore game and returnfalse.

        4. Restore value of current field to 0 (meaning it is blank).

    • False

      1. Call this method recursively and instantly check against the returned value.

        • True -> continue (resulting in returning true).

        • False -> return false.

  1. Return true.


Download 1,4 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish