using Calcoolation.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calcoolation.WinUI
{
/// <summary>
/// Main board form.
/// </summary>
public partial class frmCalcoolation : Form
{
#region attributes
int size = 4;
CellBox[,] cellBoxes;
Cell[,] matrix;
System.Drawing.Point startPoint = new System.Drawing.Point(10, 28);
int cellSize = 60;
int currentX = 0;
int currentY = 0;
bool showCandidates = false;
#endregion attributes
#region constructor
public frmCalcoolation()
{
InitializeComponent();
ResizeBoard(size);
}
#endregion constructor
#region methods
private void InitializeCellBoxes()
{
if (cellBoxes == null)
cellBoxes = new CellBox[size, size];
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
CellBox ctrCell = cellBoxes[column, row];
if (cellBoxes[column, row] == null)
{
ctrCell = new CellBox();
}
ctrCell.Column = column;
ctrCell.Row = row;
ctrCell.BackColor = System.Drawing.Color.White;
ctrCell.Location = new System.Drawing.Point(startPoint.X + (this.cellSize - 2) * column, startPoint.Y + (this.cellSize - 2) * row);
ctrCell.Name = "ctrCell" + column.ToString() + row.ToString();
ctrCell.Size = new System.Drawing.Size(cellSize, cellSize);
ctrCell.TabIndex = 2;
ctrCell.TxtClick += new EventHandler(txtBox_Click);
ctrCell.TxtKeyPress += new KeyPressEventHandler(txtBox_KeyPress);
ctrCell.TxtEnter += new EventHandler(txtBox_Enter);
ctrCell.TxtTextChanged += new EventHandler(ctrCell_TxtTextChanged);
ctrCell.TxtKeyDown += new KeyEventHandler(txtBox_KeyDown);
ctrCell.Dimension = size;
cellBoxes[column, row] = ctrCell;
this.Controls.Add(ctrCell);
cellBoxes[column, row].Tag = "";
cellBoxes[column, row].CellText = "";
cellBoxes[column, row].BringToFront();
cellBoxes[column, row].ShowCandidates = showCandidates;
cellBoxes[column, row].ShowCandidate1 =
cellBoxes[column, row].ShowCandidate2 =
cellBoxes[column, row].ShowCandidate3 = true;
cellBoxes[column, row].ShowCandidate4 = size > 3;
cellBoxes[column, row].ShowCandidate5 = size > 4;
cellBoxes[column, row].ShowCandidate6 = size > 5;
}
}
}
private void InitializeBoard()
{
ShowBoard();
ShowCages();
}
private void ShowBoard()
{
InitializeCellBoxes();
Board.Instance.GenerateBoard(size);
matrix = Board.Instance.Matrix;
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
cellBoxes[column, row].TextEnabled = true;
cellBoxes[column, row].Left1 = true;
cellBoxes[column, row].Right1 = true;
cellBoxes[column, row].Upper1 = true;
cellBoxes[column, row].Lower1 = true;
cellBoxes[column, row].strOperation = "";
cellBoxes[column, row].CellText = "";
cellBoxes[column, row].Tag = matrix[column, row].CellValue;
}
}
}
private void ShowCages()
{
List<Cage> cages = Board.Instance.Cages;
foreach (Cage cage in cages)
{
ShowCage(cage);
}
}
private void ShowCage(Cage cage)
{
foreach (Cell cell in cage.CellList)
{
bool leftBorder = true;
bool rightBorder = true;
bool upperBorder = true;
bool lowerBorder = true;
foreach (Cell cell2 in cage.CellList)
{
if (cell2.Column != cell.Column || cell2.Row != cell.Row)
{
if (cell2.Column == cell.Column - 1 && cell2.Row == cell.Row)
leftBorder = false;
if (cell2.Column == cell.Column + 1 && cell2.Row == cell.Row)
rightBorder = false;
if (cell2.Row == cell.Row - 1 && cell2.Column == cell.Column)
upperBorder = false;
if (cell2.Row == cell.Row + 1 && cell2.Column == cell.Column)
lowerBorder = false;
}
}
cellBoxes[cell.Column, cell.Row].LeftBold = leftBorder;
cellBoxes[cell.Column, cell.Row].RightBold = rightBorder;
cellBoxes[cell.Column, cell.Row].UpperBold = upperBorder;
cellBoxes[cell.Column, cell.Row].LowerBold = lowerBorder;
}
Cell firstCell = null;
string strOperation = "";
switch (cage.Operation)
{
case Operations.Plus:
strOperation = "+";
break;
case Operations.Minus:
strOperation = "-";
break;
case Operations.Multiply:
strOperation = "x";
break;
case Operations.Divide:
strOperation = "÷";
break;
}
int minPos = 100;
int pos = 0;
foreach (Cell cell in cage.CellList)
{
pos = cell.Column + cell.Row;
if (pos < minPos)
{
firstCell = cell;
minPos = pos;
}
}
cellBoxes[firstCell.Column, firstCell.Row].strOperation = cage.Result.ToString() + strOperation;
}
private void ShowResults()
{
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
string value = matrix[column, row].CellValue;
cellBoxes[column, row].ShowCandidates = false;
cellBoxes[column, row].ShowResult();
}
}
}
private bool TestResults()
{
return Board.Instance.TestResult();
}
private void ResizeBoard(int newSize)
{
if (cellBoxes != null)
{
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
if (cellBoxes[column, row] != null)
cellBoxes[column, row].Dispose();
}
}
cellBoxes = null;
}
size = newSize;
this.Width = (cellSize - 2) * size + 28;
this.Height = (cellSize - 2) * size + 120;
InitializeCellBoxes();
InitializeBoard();
}
#endregion methods
#region events
void ctrCell_TxtTextChanged(object sender, EventArgs e)
{
CellBox cellBox = (CellBox)sender;
int result;
if (Int32.TryParse(((CellBox)sender).CellText, out result))
{
Board.Instance.Matrix[cellBox.Column, cellBox.Row].UserValue = result;
}
if (TestResults() && cellBox.TextEnabled)
{
MessageBox.Show("Congratulations!!!\r\nClick 'New Game' to clear the game board");
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
matrix[column, row].CellValue = matrix[column, row].UserValue.ToString();
cellBoxes[column, row].Tag = matrix[column, row].UserValue.ToString();
}
}
ShowResults();
}
}
void txtBox_TextChanged(object sender, EventArgs e)
{
}
void txtBox_Enter(object sender, EventArgs e)
{
}
void txtBox_KeyDown(object sender, KeyEventArgs e)
{
if (showCandidates)
{
e.Handled = true;
return;
}
Control ctr = this.ActiveControl;
if (ctr is CellBox)
{
CellBox cellBox = (CellBox)sender;
currentX = cellBox.Column;
currentY = cellBox.Row;
switch (e.KeyCode)
{
case Keys.Up:
if (currentY > 0)
currentY--;
cellBoxes[currentX, currentY].Focus();
break;
case Keys.Down:
if (currentY < size - 1)
currentY++;
cellBoxes[currentX, currentY].Focus();
break;
case Keys.Left:
if (currentX > 0)
currentX--;
cellBoxes[currentX, currentY].Focus();
break;
case Keys.Right:
if (currentX < size - 1)
currentX++;
cellBoxes[currentX, currentY].Focus();
break;
}
}
}
void txtBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString().CompareTo("1") < 0 || e.KeyChar.ToString().CompareTo(size.ToString()) > 0 || showCandidates)
{
e.Handled = true;
}
else
{
matrix[currentX, currentY].UserValue = Convert.ToInt32(e.KeyChar.ToString());
}
}
void txtBox_Click(object sender, EventArgs e)
{
}
private void btnNewGame_Click(object sender, EventArgs e)
{
InitializeBoard();
}
private void btnShowResults_Click(object sender, EventArgs e)
{
ShowResults();
}
private void frmCalcoolation_Activated(object sender, EventArgs e)
{
}
private void mnuSize3X3_Click(object sender, EventArgs e)
{
ResizeBoard(3);
}
private void mnuSize4x4_Click(object sender, EventArgs e)
{
ResizeBoard(4);
}
private void mnuSize5x5_Click(object sender, EventArgs e)
{
ResizeBoard(5);
}
private void mnuSize6x6_Click(object sender, EventArgs e)
{
ResizeBoard(6);
}
private void mnuShowCandidates_Click(object sender, EventArgs e)
{
this.showCandidates = mnuShowCandidates.Checked;
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
cellBoxes[column, row].ShowCandidates = showCandidates;
}
}
}
#endregion events
}
}