In this task, we will be creating a simple game of Circle and Cross using the B4X programming language. The game will be played on a grid, and the players will take turns placing their respective symbols (circle or cross) on the grid. The objective of the game is to get three of your symbols in a row, either horizontally, vertically, or diagonally.
language-b4xSub Process_Globals 'Declare the variables and objects here Dim grid(2, 2) As Int 'The game grid Dim currentPlayer As Int 'The current player (1 for circle, 2 for cross) End Sub Sub AppStart (Form1 As Form, Args() As String) 'Initialize the game grid and set the current player For i = 0 To 2 For j = 0 To 2 grid(i, j) = 0 '0 represents an empty cell Next Next currentPlayer = 1 'Circle starts first 'Add the game grid to the form For i = 0 To 2 For j = 0 To 2 Dim btn As Button btn.Initialize("btn") btn.Tag = i & "," & j 'Store the cell coordinates in the button's tag btn.Text = "" btn.SetLayout(i * 100dip, j * 100dip, 100dip, 100dip) Form1.AddView(btn, i * 100dip, j * 100dip, 100dip, 100dip) Next Next End Sub Sub btn_Click Dim btn As Button = Sender Dim coordinates() As String = btn.Tag.Split(",") Dim row As Int = coordinates(0) Dim col As Int = coordinates(1) 'Check if the clicked cell is empty If grid(row, col) = 0 Then 'Place the current player's symbol on the grid grid(row, col) = currentPlayer 'Update the button's text with the current player's symbol If currentPlayer = 1 Then btn.Text = "O" Else btn.Text = "X" End If 'Check if the current player has won If CheckWin(currentPlayer) Then ToastMessageShow("Player " & currentPlayer & " wins!", True) 'Reset the game ResetGame() Else 'Switch to the next player currentPlayer = 3 - currentPlayer End If Else ToastMessageShow("Invalid move!", True) End If End Sub Sub CheckWin(player As Int) As Boolean 'Check for a winning combination in rows, columns, and diagonals For i = 0 To 2 If grid(i, 0) = player And grid(i, 1) = player And grid(i, 2) = player Then Return True 'Check rows If grid(0, i) = player And grid(1, i) = player And grid(2, i) = player Then Return True 'Check columns Next If grid(0, 0) = player And grid(1, 1) = player And grid(2, 2) = player Then Return True 'Check diagonal If grid(0, 2) = player And grid(1, 1) = player And grid(2, 0) = player Then Return True 'Check anti-diagonal Return False End Sub Sub ResetGame 'Reset the game grid and the current player For i = 0 To 2 For j = 0 To 2 grid(i, j) = 0 Dim btn As Button = Form1.GetView(i * 3 + j) btn.Text = "" Next Next currentPlayer = 1 End Sub
The code provided above creates a Circle and Cross game using the B4X programming language. Here's a breakdown of how the code works:
The Process_Globals
section declares the necessary variables and objects for the game. The grid
variable represents the game grid, and the currentPlayer
variable keeps track of the current player.
The AppStart
sub initializes the game grid and sets the current player. It also adds buttons to the form to represent the cells of the grid. Each button is assigned a tag that stores its coordinates on the grid.
The btn_Click
sub is triggered when a button is clicked. It checks if the clicked cell is empty and places the current player's symbol on the grid. The button's text is updated accordingly. It then checks if the current player has won by calling the CheckWin
function. If a player wins, a toast message is displayed, and the game is reset.
The CheckWin
function checks for winning combinations in rows, columns, and diagonals. If a winning combination is found, it returns True
; otherwise, it returns False
.
The ResetGame
sub resets the game grid and the current player. It clears the text of all buttons and sets the current player back to 1.
By following the steps above, you can create a simple Circle and Cross game using the B4X programming language. Feel free to modify the code to add additional features or customize the game to your liking. Happy coding!