473,508 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

4*4 Board Game

1 New Member
I'm writing a code for a board game.

To play the game, a player generates two random numbers and colours in an area on the grid indicated by the numbers. For example if the generated number is a 2 and a 3 the player clicks in the 2x3 square which results in the colour of the square being changed.

A player should only be able to click and change the colour of the square represented by the random numbers only.

At the start of the game, the initial score should have a value of 0. With each square coloured the score increases by the product of two numbers. You can display the score anywhere within the user interface.

The game should maintain the time taken to colour all the squares. Hence, when a player starts the game the timer should also start. You can display the time anywhere within the user interface.

The game should end in two ways. They include: a. A player generating the same random numbers in three consecutive attempts b. A player colouring all the squares

The second player should be able to play the game without closing the whole application. When the game ends for the second player, the winner should be announced. The winner should be the one who scores the highest within the least amount of time. If a player has the highest score but takes the highest time, then the game is considered a tie. Hence, display a message saying it is a tie.

Here is the code:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Grid(3, 3) As Button
  3.     Private btnSize As Integer = 25
  4.     Private btnPosn As New Point(10, 10)
  5.  
  6.     Private randomRow As Integer
  7.     Private randomCol As Integer
  8.  
  9.  
  10.     Shared random As New Random()
  11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  12.  
  13.         'Random Number Generator
  14.         Dim i As Integer
  15.         For i = 0 To 5
  16.             TextBox1.Text = (Convert.ToString(random.Next(1, 5)))
  17.             TextBox2.Text = (Convert.ToString(random.Next(1, 5)))
  18.  
  19.         Next
  20.  
  21.     End Sub
  22.  
  23.     Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
  24.     Dim stpw As Stopwatch = Stopwatch.StartNew
  25.  
  26.     Private Sub tick(ByVal state As Object)
  27.         'Timer
  28.         If stpw.IsRunning Then
  29.  
  30.             Me.Invoke(Sub()
  31.                           Label1.Text = stpw.Elapsed.ToString("hh\:mm\:ss\.")
  32.                       End Sub)
  33.         End If
  34.  
  35.     End Sub
  36.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  37.         CreateGrid()
  38.     End Sub
  39.     Private Sub CreateGrid()
  40.         'Create a 4x4 grid of buttons with a common event handler. References
  41.         'to the buttons are stored in "Grid" for easy access. The row and
  42.         'column numbers are stored in the button tags to differentiate the
  43.         'individual buttons in the click handler.
  44.         For row As Integer = 0 To 3
  45.             For col As Integer = 0 To 3
  46.                 Dim xpos As Integer = btnPosn.X + btnSize * col
  47.                 Dim ypos As Integer = btnPosn.Y + btnSize * row
  48.                 Dim btn As New Button()
  49.                 btn.Tag = {row, col}
  50.                 btn.Location = New Point(xpos, ypos)
  51.                 btn.Size = New Size(btnSize, btnSize)
  52.                 AddHandler btn.Click, AddressOf Grid_Click
  53.                 Me.Controls.Add(btn)
  54.                 Grid(row, col) = btn
  55.             Next
  56.         Next
  57.     End Sub
  58.     Dim ClickCount As Integer = 0
  59.  
  60.     Private Sub Grid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  61.         Dim btn As Button = sender
  62.         Dim row As Integer = btn.Tag(0)
  63.         Dim col As Integer = btn.Tag(1)
  64.         Me.Text = "you clicked cell (" & row & "," & col & ")"
  65.  
  66.  
  67.  
  68.         'Color Change
  69.             If Not ClickCount = 4 Then
  70.                 ClickCount += 1
  71.             Else
  72.                 ClickCount = 1
  73.             End If
  74.  
  75.             Select Case ClickCount
  76.                 Case Is = 1
  77.                     Grid(row, col).BackColor = Color.Green
  78.                 Case Is = 2
  79.                     Grid(row, col).BackColor = Color.Yellow
  80.                 Case Is = 3
  81.                     Grid(row, col).BackColor = Color.Red
  82.  
  83.             End Select
  84.  
  85.  
  86.  
  87.  
  88.  
  89.     End Sub
  90.  
  91.  
  92.  
I'm unable to change the colour of the square represented by the random numbers only. and I'm also having difficulty at score keeping.
Sep 6 '15 #1
0 1199

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4603
by: Tom Alcendor | last post by:
I am building a chess game application that allows a user to click on a piece and move it to a new square. When the user clicks on a piece the mouse pointer must change to the image associated...
0
1960
by: Supra | last post by:
i got board working using graphic window in vb.net but no controls adding to form. i am doing checker board game. when i clicked and moved the peg to another location(grid). but how do i get bitmap...
2
4450
by: ck388 | last post by:
I was wondering if anyone could answer a couple of questions regarding developing games. I would like to create one for fun on my spare time. I want to know what type of technologies I would have...
0
1983
by: Supra | last post by:
i got board working using graphic window in vb.net but no controls adding to form. i am doing checker board game. when i clicked and moved the peg to another location(grid). but how do i get bitmap...
6
2818
by: Supra | last post by:
i got board working using graphic window in vb.net but no controls adding to form. i am doing checker board game. when i clicked and moved the peg to another location(grid). but how do i get bitmap...
10
12535
by: sam_cit | last post by:
Hi Everyone, I'm working on developing a chess game and i decided to use c++ for its object oriented approach. I have a bass class unit and is inherited to distinct number of units (like king,...
4
20644
by: Hypnotik | last post by:
So I'm writing this program. I have the board constructed, and I'm having a problem making a move. At this point I just want to make a move, I'll work on whether it is a legal move after that. The...
4
4422
by: COHENMARVIN | last post by:
Are there any good sources on video game programming in vb.net? Is "DirectX" a set of libraries for video game programmers? The reason I ask is that I'd like to convert a board game into a...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.