C# piškvorky - kde môže byť chyba?
PlayBoard.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Piškvorky.GamePiece;
namespace Piškvorky
{
public partial class PlayBoard : UserControl
{
private int boardSize = 19;
private int fieldSize = 20;
private Color colorGrid = Color.Gray;
private Color colorX = Color.Red;
private Color colorO = Color.Green;
private GamePiece[,] piecesOnBoard;
public GamePiece[,] PiecesOnBoard
{
get
{
if (piecesOnBoard == null)
ClearBoard();
return piecesOnBoard;
}
}
private void ClearBoard()
{
piecesOnBoard = new GamePiece[boardSize, boardSize];
for (int x = 0; x < boardSize; x++)
for (int y = 0; y < boardSize; y++)
piecesOnBoard[x, y] = Free;
}
public int BoardSize
{
get { return boardSize; }
set
{
boardSize = value;
Refresh();
}
}
public int FieldSize
{
get { return fieldSize; }
set
{
fieldSize = value;
Refresh();
}
}
public Color ColorGrid
{
get { return colorGrid; }
set
{
penGrid = null;
colorGrid = value;
Refresh();
}
}
#region Pens
private Pen penGrid;
public Pen PenGrid
{
get
{
if (penGrid == null)
penGrid = new Pen(colorGrid);
return penGrid;
}
}
private Pen penX;
public Pen PenX
{
get
{
if (penX == null)
penX = new Pen(colorX);
return penX;
}
}
private Pen penO;
public Pen PenO
{
get
{
if (penO == null)
penO = new Pen(colorO);
return penO;
}
}
#endregion
public PlayBoard()
{
InitializeComponent();
}
private void PlayBoard_Paint(object sender, PaintEventArgs e)
{
DrawBoard(e.Graphics);
}
private void DrawBoard(Graphics g)
{
for (int i = 0; i <= boardSize; i++)
{
g.DrawLine(PenGrid, 0, i * fieldSize, boardSize * fieldSize, i * fieldSize);
g.DrawLine(PenGrid, i * fieldSize, 0, i * fieldSize, boardSize * fieldSize);
}
}
private void DrawPieces(Graphics g)
{
for (int x = 0; x < boardSize; x++)
for (int y = 0; y < boardSize; y++)
DrawPiece(g, PiecesOnBoard[x,y], x, y);
}
private void DrawPiece(Graphics g, GamePiece piece, int x, int y)
{
if (!(x < boardSize && x >= 0 && y < boardSize && y >= 0))
throw new Exception("Súradnice sú mimo hraciu plochu");
if (piece == X)
{
g.DrawLine(PenX,
x * fieldSize +1,
y * fieldSize +1,
x * fieldSize + fieldSize -1,
y * fieldSize + fieldSize -1);
g.DrawLine(PenX,
x * fieldSize + 1,
y * fieldSize + fieldSize - 1,
x * fieldSize + fieldSize - 1,
y * fieldSize + 1);
}
if (piece == O)
{
g.DrawEllipse(PenO,
x * fieldSize + 1,
y * fieldSize + 1,
fieldSize - 2,
fieldSize - 2);
}
}
private void PlayBoard_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X/fieldSize;
int y = e.Y/fieldSize;
if (!(x < boardSize && x >= 0 && y < boardSize && y >= 0))
return;
piecesOnBoard[x, y] = GamePiece.X;
Refresh();
}
}
}
GamePiece.cs
namespace Piškvorky
{
public enum GamePiece
{
Free,
X,
O,
}
}
---
Vím není to dokončené ale podle toho čo tam zatím je by malo to ísť spustiť a v prípade klikania všade hádzať len X, no mne to v poho otvori ale po kliknuti na nejake poličko apka sekne a vyskočí mi tohle:
[img=http://s18.postimg.org/ulqnn1urt/Bez_n_zvu.png]Bez_n_zvu.png[/img]
Viem vlastne čo to piše, ale i tak by to malo fungovať ne?
Píše to, že piecesOnBoard je null. Bez toho, aby si tú premennú predtým nastavil, to fungovať nebude. Zrejme si chcel pristupovať k PiecesOnBoard a nie k piecesOnBoard.
Díky pristupovať som chcel na piecesOnBoard, ale zabudol som dopsať
zvýraznený riadok v tejto časti, ted to už jede.
To není moc dobrý přístup. Inicializaci bys měl dělat přímo a ne jako side effect.
funguje to tak to zatím nechám tak, zajtra uvidím čo môžem vylepšiť.