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?
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/
As the sudoku solving contest winner I will now explain the solution I created. I must say that my solution is highly based on a solution I found in a blog post from a canadian student on the internet. Though I took a ready and very quick algorithm there was still many work to do. So this...
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.
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.
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.
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.
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
Define the levels:
Create different levels with pre-filled numbers by initializing the 2D array for each level. For example:
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
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.
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.
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
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
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
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.
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?
The attached project will generate and solve Sudoku puzzles (9x9). The code to solve the puzzles with was adapted from here (converted to B4A). The code to generate Sudoku puzzles with comes from here. It was adapted and then compiled to a B4A library. The library files (jar and xml) are in the...
Just for the fun of it.... Button Create = uses class dlx_generator to create a new puzzle Button New Puzzle = use a variant of 1 of the 742 puzzles embedded in the code (randomly swapping columns/rows to create more than the 742 puzzles) Button Solve = obviously solving the current puzzle. It...
1. Copy the attached jar to your B4J additional library folder
2. Sample B4J project attached
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
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
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.
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ù.
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.
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.
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)
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.