Added proprietary user interface.

Added most game logic.
This commit is contained in:
Michael Chen
2020-01-04 13:13:13 +01:00
committed by Michael Chen
parent 71c1a4d20c
commit 27f6efb58a
8 changed files with 369 additions and 58 deletions

View File

@ -62,8 +62,6 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Grid.cs" />
<Compile Include="Properties\Player.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
@ -81,5 +79,11 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TicTacToe\TicTacToe.csproj">
<Project>{8EB373A9-D083-4BFE-9396-E9A40E75AAC2}</Project>
<Name>TicTacToe</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -23,20 +23,46 @@
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent() {
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.TslStatus = new System.Windows.Forms.ToolStripStatusLabel();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// statusStrip1
//
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(32, 32);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.TslStatus});
this.statusStrip1.Location = new System.Drawing.Point(0, 853);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(1010, 38);
this.statusStrip1.TabIndex = 0;
this.statusStrip1.Text = "statusStrip1";
//
// TslStatus
//
this.TslStatus.Name = "TslStatus";
this.TslStatus.Size = new System.Drawing.Size(0, 28);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1010, 891);
this.Controls.Add(this.statusStrip1);
this.Name = "MainForm";
this.Text = "Form1";
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel TslStatus;
}
}

View File

@ -1,21 +1,59 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TicTacToe;
namespace CleanTicTacToe {
public partial class MainForm : Form {
private const int BUTTONSIZE = 100;
private readonly Grid _grid;
private readonly Button[,] _pbxs = new Button[Grid.GRIDSIZE, Grid.GRIDSIZE];
public MainForm() {
InitializeComponent();
_grid = new Grid();
_grid.ExpectNextTurn += Grid_ExpectNextTurn;
_grid.GameOver += Grid_GameOver;
for (var x = 0; x < Grid.GRIDSIZE; x++)
for (var y = 0; y < Grid.GRIDSIZE; y++) {
var pictureBox = new Button() {
Location = new Point(x * BUTTONSIZE, y * BUTTONSIZE),
Width = BUTTONSIZE,
Height = BUTTONSIZE,
Tag = (x, y)
};
pictureBox.Click += Grid_Click;
Controls.Add(pictureBox);
_pbxs[x, y] = pictureBox;
}
}
private void Grid_GameOver(object sender, Player e) {
TslStatus.Text = $"Player {e} has won!";
_grid.Reset();
}
private void Grid_ExpectNextTurn(object sender, Player e) {
TslStatus.Text = $"Player {e} must now pick a spot!";
}
private void Grid_Click(object sender, EventArgs e) {
if (!(sender is Button pictureBox))
throw new InvalidProgramException("Expected picturebox as sender!");
(var x, var y) = ((int x, int y))pictureBox.Tag;
TslStatus.Text = $"Spot {x},{y} clicked!";
try {
_grid.PickSpot(x, y);
} catch (Exception ex) {
TslStatus.Text = ex.Message;
}
RefreshPbxs();
}
private void RefreshPbxs() {
for (var x = 0; x < Grid.GRIDSIZE; x++)
for (var y = 0; y < Grid.GRIDSIZE; y++) {
_pbxs[x, y].Text = $"{Grid.GetPlayerChar(_grid[x, y])}";
}
}
}
}

View File

@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -1,26 +0,0 @@
namespace TicTacToe {
public class Grid {
private readonly Player[,] _field;
private const int GRIDSIZE = 3;
public bool GameOver => IsGameOver();
private bool IsGameOver() {
for (var x = 0; x < GRIDSIZE; x++)
for (var y = 0; y < GRIDSIZE; y++)
if (IsFieldEmpty(x, y))
return false;
return true;
}
private bool IsFieldEmpty(int x, int y) {
return _field[x, y] == Player.Empty;
}
public Grid() {
_field = new Player[GRIDSIZE, GRIDSIZE];
}
public bool PickSpot() {
}
}
}

View File

@ -1,7 +0,0 @@
namespace TicTacToe {
enum Player {
Empty = 0,
Cross = 1,
Circle = 2
}
}