Android Question I'm trying to make a sudoku game in android but I'm new in programming

kleiny

New Member
Hi everyone, I was thinking of creating a game of sudoku but I don't know where to start I thought about making the grids for it first and search for some ways to do it and didn't find a way to make some work. I thought of making it by levels like I'll put the numbers myself for each level and not something that is generated. Back to the question is there a way to make grids easily and input will be able to input numbers in b4a?
 

Sagenut

Expert
Licensed User
Longtime User
Welcome aboard!
It's nice and good to see such as entusiasm, but maybe before pointing a such far and important target you should start learning the basics of programming like Subs, Classes, passing parameters, returning parameters, events, cycles.....
Notice:
Checking examples and tutorials you will find older ones based on Activity Projects (called Default in B4A) and B4XPages projects.
Nowadays you should stick to learn and use immediately B4XPages and avoid Activity Projects.
Of course I don't know your actual level so I could be exaggerating, or maybe not. ;)
Something to begin
https://www.b4x.com/android/forum/threads/b4x-documentation-booklets.88985/
 
Last edited:
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Upvote 0

aeric

Expert
Licensed User
Longtime User
Why reinvent the wheel where there are some posts already exist in this forum?
Familiar yourself with the forum search engine.

Here is an example:
 
Upvote 1

DonManfred

Expert
Licensed User
Longtime User
Here is an example
In #1 he told to write a SODUKO-Game. Not a solver. So he is evaluating the Views he can use for such a Game i guess.
 
Upvote 0

kleiny

New Member
Thank you! Sagenut and Jeffrey I'll try to learn more about b4a first. I thought it would be easy with the sudoku related stuff in the forum but all I see are solvers and generators which are too complicated for me.
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I suggest, take any one sample from numerous Sudoku samples available on the Forum.
Just study and understand the code to make the grid and accessing the grid elements.

Focus only on this part for your game. You will be good.
 
Upvote 0

Lee Gillie

New Member
Licensed User
Longtime User
I was curious if ChatGPT could answer the original question. Here is the AI reponse:

Creating a Sudoku game in B4A (Basic4android) is a great idea! To help you get started, I'll provide a basic outline of the steps you can take to create the grids and input numbers. Since you'd like to create the levels yourself, you can store the pre-filled grids as arrays.

  1. Design the user interface: Use the B4A Designer to create a 9x9 grid layout for the Sudoku board. You can use a Table (with a 9x9 GridLayout) and place EditTexts (or Labels for fixed numbers) in each cell. Make sure to set inputType of EditTexts to 'number' to restrict input to only numbers.
  2. Create a 2D array for storing the Sudoku grid: You can create a 2D array (9x9) to store the initial state of the Sudoku grid for each level. For example:
    B4X:
    Dim SudokuGrid(9, 9) As Int
  3. Define the levels:
    Create different levels with pre-filled numbers by initializing the 2D array for each level. For example:
    B4X:
    Sub Level1()
        SudokuGrid = Array As Int( _
            5, 3, 0, 0, 7, 0, 0, 0, 0, _
            6, 0, 0, 1, 9, 5, 0, 0, 0, _
            0, 9, 8, 0, 0, 0, 0, 6, 0, _
            8, 0, 0, 0, 6, 0, 0, 0, 3, _
            4, 0, 0, 8, 0, 3, 0, 0, 1, _
            7, 0, 0, 0, 2, 0, 0, 0, 6, _
            0, 6, 0, 0, 0, 0, 2, 8, 0, _
            0, 0, 0, 4, 1, 9, 0, 0, 5, _
            0, 0, 0, 0, 8, 0, 0, 7, 9)
    End Sub
  4. Populate the UI grid with the level data:
    Create a subroutine to populate the EditTexts (or Labels) with the numbers from the 2D array. For example:
    B4X:
    Sub PopulateGrid()
        For row = 0 To 8
            For col = 0 To 8
                Dim cell As EditText = Table1.GetView(row * 9 + col)
                If SudokuGrid(row, col) > 0 Then
                    cell.Text = SudokuGrid(row, col)
                    cell.Enabled = False ' Disable input for pre-filled numbers
                Else
                    cell.Text = ""
                    cell.Enabled = True ' Enable input for empty cells
                End If
            Next
        Next
    End Sub
  5. Implement input validation: You can add a TextChanged event for each EditText to validate the user's input and check if the number is valid for the row, column, and 3x3 square.
  6. Add a mechanism for switching levels: Implement a level selection feature that allows users to switch between different levels. You can use buttons or a spinner for this purpose. Once a level is selected, call the corresponding level subroutine (e.g., Level1()) and then call PopulateGrid() to update the UI.
  7. B4X:
    Sub IsSolutionCorrect() As Boolean
        For row = 0 To 8
            For col = 0 To 8
                Dim cell As EditText = Table1.GetView(row * 9 + col)
                Dim num As Int = cell.Text
                If Not IsValidMove(row, col, num) Then
                    Return False
                End If
            Next
        Next
        Return True
    End Sub
  8. Add a "Check Solution" button:
    Place a button on the screen for users to check their solution. In the Click event for this button, call the IsSolutionCorrect() subroutine to validate the user's input. If the solution is correct, display a congratulatory message; if not, show an error message.
    B4X:
    Sub CheckSolutionButton_Click
        If IsSolutionCorrect() Then
            ToastMessageShow("Congratulations! You've solved the puzzle!", True)
        Else
            ToastMessageShow("Oops! There's an error in your solution. Please try again.", True)
        End If
    End Sub
  9. Add a "Clear Board" or "Reset" button:
    To allow users to start over, add a "Clear Board" or "Reset" button that resets the Sudoku grid to the initial state of the current level. This can be done by calling the PopulateGrid() subroutine again.
    B4X:
    Sub ClearBoardButton_Click
        PopulateGrid()
    End Sub
  10. Enhance the user experience:
    To improve the user experience, consider adding features such as a timer, a hint system, or an option to display the correct solution. Additionally, you can customize the appearance of the grid, such as highlighting the selected cell or differentiating between editable and non-editable cells with different colors.
Once you have implemented these steps, you'll have a basic Sudoku game with pre-defined levels that users can play and interact with in B4A.

How do you think it did? Should I be worried?
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Hi everyone, I was thinking of creating a game of sudoku but I don't know where to start I thought about making the grids for it first and search for some ways to do it and didn't find a way to make some work. I thought of making it by levels like I'll put the numbers myself for each level and not something that is generated. Back to the question is there a way to make grids easily and input will be able to input numbers in b4a?
Another one here


And one here:
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Another one - just for fun (B4J)

1. Copy the attached jar to your B4J additional library folder
2. Sample B4J project attached

1.png


This is the entire B4J code:

B4J code:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: SudokuV3.0

Sub Process_Globals
    Private fx As JFX
'    Private MainForm As Form
    Private xui As XUI
   
    Dim ms As JavaObject

End Sub

Sub AppStart (Form1 As Form, Args() As String)
'    MainForm = Form1
'    MainForm.RootPane.LoadLayout("Layout1")
'    MainForm.Show

    ms.InitializeNewInstance("MySudoku", Null)
    Dim aa() As String = Array As String("")
    ms.RunMethod("main", Array(aa))
   
End Sub
 

Attachments

  • SudokuV3.0.jar
    7 KB · Views: 63
  • b4jSudokuV3.0.zip
    2 KB · Views: 66
Last edited:
Upvote 0

kleiny

New Member
Hello everyone thank you for all the answers that you've given me. My progress now is that I've put a panel which has the numbers on it that I manually inserted and some buttons that still doesn't have some functionality on them. Right now I'm having a problem on how to press one square on the grid and pressing the button will show the number as well as how do I check it or how will the code check the number that has been input correct or wrong. As I've seen on the other examples that you guys provided some of them uses something java related is there a way to purely just use b4a for it? I'm thinking on just putting everything on just an edit text
 

Attachments

  • Sudoku_Code_fornow.png
    Sudoku_Code_fornow.png
    62.5 KB · Views: 74
Upvote 0

kleiny

New Member
So I gave up, the one thing that I thought of now is to put edittext on all the boxes now I don't know how function the buttons to put numbers on the edittexts, because if I I press one of the buttons it shows to all edittexts.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Attach your project so it's possible to check it and help you.
If you made a B4XPages project you must keep pressed CTRL on keyboard and click this line in your code
B4X:
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
Before doing this change Project.zip to something with more sense. :)
If instead you made a Default project (Activity project) simply use Export as Zip from File MenĂą.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
So I gave up ...
So, what did you give up, exactly? Did you give up writing a Sudoku application, or did you give up trying to learn how to write a program? They are not the same. As @Jeffrey Cameron said at the beginning, writing a Sudoku application is a not the best place to start. If you are finding it tough do not let that put you off learning to code; just understand that you will progress in small steps and you have a long way to go.

I have written several Sudoku apps. I always used a canvas to represent the playing area. Here is a simple example. The input buttons at the bottom are buttons, the blue completion indicators are labels, and the playing area is a canvas. In time you will have to learn about all of these. Do not give up yet, but try and be patient.

Sample.jpg
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
So I gave up, the one thing that I thought of now is to put edittext on all the boxes now I don't know how function the buttons to put numbers on the edittexts, because if I I press one of the buttons it shows to all edittexts.

This is based on my sample project here.

Have added some code to be able to "play sudoku" in B4J. You can study the code and amend it to be used in B4A (mostly UI changes that might be required).
Simple to use:
1. Click on a non-given cell - the cell's background will be animated to transparent
2. Click on one of the numbers below the grid to select that number for the cell that you have selected
3. If you selected the correct number for that cell the cell will be filled with that value and the opacity set back to opaque - else the cell opacity will stay transparent. You can keep on trying different numbers for a selected cell. At one time or another you will select the correct value and the value will be displayed in the selected cell and opacity will be restored.
4. If you selected a cell and want to de-select it then just click on the same cell again.

I have not added code to check when the puzzle has been completed successfully - you can do so (all rows and all columns must add to 45 as a minimum check)

1680428209506.png
 

Attachments

  • b4jSudokuStrippedBare.zip
    35.6 KB · Views: 65
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Just a word of encouragement. It is EXTREMELY frustrating in the beginning to learn a new programming language when not doing formal training. EVERYTHING feels non-intuitive.

Stick with it. It will get easier.
 
Upvote 0
Top