Working on a simple Tic-Tac-Toe Simulator and this is my code for the click event
private void newGame_button_Click(object sender, EventArgs e)
{
Random rand = new Random();
double randNum = rand.Next(0, 2);
const int ROWS = 3;
const int COLS = 3;
string[,] ticTacToe_2dArray = new string[ROWS, COLS]
{
{displayTTT_label1.Text, displayTTT_label2.Text, displayTTT_label3.Text},
{displayTTT_label4.Text, displayTTT_label5.Text, displayTTT_label6.Text},
{displayTTT_label7.Text, displayTTT_label8.Text, displayTTT_label9.Text}
};
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
randNum = rand.Next(0, 2);
if (randNum == 0)
{
ticTacToe_2dArray[row, col] = "O";
}
else if (randNum == 1)
{
ticTacToe_2dArray[row, col] = "X";
}
}
}
}
Label Grid
Trying to pair the result of the conditional test that assigns an X/O to the next corresponding label.Text variable on my label grid. I am pretty sure the current code is just changing the array variable to X/O instead of the label.Text value.
Your problem is that you are storing copies of strings from your text boxes and then updating the copies.
What you need to do is store the text boxes in the dictionary and assign the "X" and "O" strings to their text properties.
private void newGame_button_Click(object sender, EventArgs e)
{
Random rand = new Random();
double randNum = rand.Next(0, 2);
const int ROWS = 3;
const int COLS = 3;
TextBox[,] ticTacToe_2dArray = new TextBox[ROWS, COLS]
{
{displayTTT_label1, displayTTT_label2, displayTTT_label3},
{displayTTT_label4, displayTTT_label5, displayTTT_label6},
{displayTTT_label7, displayTTT_label8, displayTTT_label9}
};
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
randNum = rand.Next(0, 2);
if (randNum == 0)
{
ticTacToe_2dArray[row, col].Text = "O";
}
else if (randNum == 1)
{
ticTacToe_2dArray[row, col].Text = "X";
}
}
}
}
Related
I was wondering how can I stop the iteration through the rows and columns of a 2d matrix if a value is found.
int[,] matrix = new int[8, 10];
Random rnd = new Random();
bool value = false;
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
int number = rnd.Next(1, matrix.Length + 1);
matrix[row, col] = number;
Console.Write("{0,3}", number);
if (number == 8)
{
value = true;
break;
}
}
}
I've been toying with this code which prints a small matrix with negative and positive numbers,
I would like an efficient way to add together only the positive elements on the matrix and get the product of such elements also do the same but after a specific number, such as the highest on the matrix
static void Main(string[] args)
{
int row = 5;
int column = 5;
int[,] array = new int[row, column];
Random rand = new Random();
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
array[i, j] = rand.Next(-5, 10);
}
}
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j].ToString() + " ");
}
Console.WriteLine(" ");
}
Console.ReadLine();
}
To filter and add positive matrix elements, you could do something like the following:
static void Main(string[] args)
{
const int rows = 5;
const int columns = 5;
var array = new int[rows, columns];
var rand = new Random();
int sum = 0;
bool numberSeen = false;
int numberToSee = 1;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
var cell = rand.Next(-5, 10);
if (!numberSeen && (cell == numberToSee))
{
numberSeen = true;
}
array[row, col] = cell;
if (numberSeen && (cell > 0))
{
sum += cell;
}
}
}
Console.WriteLine($"sum = {sum}");
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
Console.Write(array[row, col].ToString() + " ");
}
Console.WriteLine(" ");
}
Console.ReadLine();
}
I have a problem. I need to count cells that I activate, they are yellow, but I dont know how i can do it. In geniral I need to select only maximum 15 cells, so i need to count them, but all my tries seems so far away. I tried to cteate a counter, but it doest work. Please, help.
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
//выделение только ячеек
// создаём массив
int[,] Array = new int[8, 10];
byte numbers = 1;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
Array[i, j] = numbers;
numbers++;
}
}
dataGridView1.RowCount = 8;
dataGridView1.ColumnCount = 10;
// программно записываем массив и задаём стиль ячеек
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
dataGridView1.Columns[j].Width = 30;
dataGridView1.Rows[i].Height = 30;
dataGridView1.Rows[i].Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Rows[i].Cells[j].Style.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
dataGridView1.Rows[i].Cells[j].Value = Array[i, j].ToString();
}
}
}
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) // выделение ячеек
{
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
if (dataGridView1.SelectedCells[i].Style.BackColor == Color.Yellow)
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.White;
}
else
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.Yellow;
}
dataGridView1.CurrentCell = null;
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
byte _selected = 0;
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
counter(_selected);
}
}
public void counter(int count)
{
count++;enter code here
MessageBox.Show(count.ToString());
}
Here is how form look.
form
The name of the game is Keno and i try to create it. Maybe i have some mistakes, sorry.
Here's my spin on your code. In this snippet I am using int yellowed to keep track of how many cells are yellow. When a user clicks on a cell, the cell counter sets the yellow count. When the mouse button is up(dataGridView1_CellMouseUp), then only the appropriate number of cells are allowed to be made yellow.
using System.Drawing;
using System.Windows.Forms;
namespace DataGridView_47478857
{
public partial class Form1 : Form
{
DataGridView dataGridView1 = new DataGridView();
int yellowed = 0;
int maxYellowed = 15;
public Form1()
{
InitializeComponent();
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.CellMouseUp += dataGridView1_CellMouseUp;
dataGridView1.CellClick += dataGridView1_CellClick;
this.Controls.Add(dataGridView1);
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
//выделение только ячеек
// создаём массив
int[,] Array = new int[8, 10];
byte numbers = 1;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
Array[i, j] = numbers;
numbers++;
}
}
dataGridView1.RowCount = 8;
dataGridView1.ColumnCount = 10;
// программно записываем массив и задаём стиль ячеек
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
dataGridView1.Columns[j].Width = 30;
dataGridView1.Rows[i].Height = 30;
dataGridView1.Rows[i].Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Rows[i].Cells[j].Style.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
dataGridView1.Rows[i].Cells[j].Value = Array[i, j].ToString();
}
}
}
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) // выделение ячеек
{
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
if (dataGridView1.SelectedCells[i].Style.BackColor == Color.Yellow)
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.White;
}
else
{
if (yellowed < maxYellowed)//only color code this cell if the yellow cell count has not been exceeded
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.Yellow;
yellowed++;
}
}
}
dataGridView1.ClearSelection();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
yellowed = 0;
foreach (DataGridViewRow currentRow in dataGridView1.Rows)
{
foreach (DataGridViewCell currentCell in currentRow.Cells)
{
if (currentCell.Style.BackColor == Color.Yellow)
{
yellowed++;
}
}
}
}
}
}
You can use this link to count cell, but you can use another components or WPF to this game.
https://msdn.microsoft.com/en-us/library/x8x9zk5a(v=vs.85).aspx
Hi i am new to programming in c# wpf i just need to create a datagrid of some details i made it and the one major task is the while adding the serial number will be incremented as 1,2,3,4,etc.as when i remove an record from the datagrid the serial number to rearrange according to it eg:sno:1,2,3,4,5,6.
after removing the 3rd row it should be 1,2,3,4,5 instaed of 1,2,4,5,6.
similarly for inserting a row between 1,2,3 after adding it should be 1,2,3,4 task is row nust be added between 2 and 3 the new row help need
void mbtninsertstep_Click(object sender, RoutedEventArgs e) {
int rowindex = mdatagridedit.Items.IndexOf(mdatagridedit.CurrentCell);
if (rowindex >= 0) {
int rowcount = programtable.Rows.Count;
msteps.Add(new Steps { mStepno = count, mPosition = "0",
mRepeat = "NONE", mCount = "1", mAftercut = "NONE" });
int p = rowindex + 1;
for (int i = 0; i < rowcount + 1; i++) {
programtable.Rows[i][0] = p++;
} edited = true;
}
}
delete button code
insert button code.
void mbtndeletestep_Click(object sender, RoutedEventArgs e)
{
int deleterow;
DataGridView dg = new DataGridView();
// msteps.Remove((Steps)mdatagridedit.SelectedItem);
int rowindex = dg.CurrentRow.Index;
if (rowindex >= 0) {
int rowcount = programtable.Rows.Count;
int temp = dg.CurrentCell.RowIndex;
programtable.Rows.RemoveAt(temp);
int p = temp + 1;
for (int i = rowindex; i < rowcount - 1; i++) {
programtable.Rows[i][0] = p++;
}
int RowCountAfterDeleting = programtable.Rows.Count;
}
//int p = mdatagridedit.Items.Count;
// if (mdatagridedit.SelectedItem == null) {
// System.Windows.Forms.MessageBox.Show("Select an row");
// } else {
//msteps.RemoveAt(mdatagridedit.SelectedIndex);
//int p = mdatagridedit.SelectedIndex + 1;
// for(int i = mdatagridedit.SelectedIndex;
i < mdatagridedit.Items.Count - 1; i++){
// Steps step = new Steps();
// step.mStepno = p - 1;
// } int p1 = programtable.Rows.Count;
//}
}
I am working on a C# program that has to copy the elements of a randomized 10*12 2D array on a 1D Array. Everything seems to be working fine. However, some of the elements of the 2D Array (last 18) would not copy to the 1D Array.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace James_Archbold_A1
{
class Program
{
static void Main(string[] args)
{
int row = 10;
int column = 12;
int[,] twoDArray = new int[row, column];
int[] oneDArray = new int[row*column];
FillTwoDimArray(twoDArray);
DisplayTwoDimArray(twoDArray);
StoreValues(twoDArray, oneDArray);
DisplayOneDimArray(oneDArray);
Console.ReadLine();
}
static void FillTwoDimArray(int[,] table)
{
int min = 0;
int max = 100;
int rndNumber;
Random rnd = new Random();
for (int row = 0; row < table.GetLength(0); row++) //use GetLength(0) to get the size of the row
{
for (int col = 0; col < table.GetLength(1); col++) //use GetLength(1) to get the size of the column
{
rndNumber = rnd.Next(min,max);
table[row, col] = rndNumber;
}
}
}
static void DisplayTwoDimArray(int[,] table)
{
for (int row = 0; row < table.GetLength(0); row++)
{
for (int col = 0; col < table.GetLength(1); col++)
{
Console.Write("{0}\t", table[row, col]);
}
}
Console.WriteLine();
}
static void StoreValues(int[,] twoDArray, int[] oneDArray)
{
int rowSize = twoDArray.GetLength(0);
int colSize = twoDArray.GetLength(1);
for (int row = 0; row < rowSize; row++)
{
for (int col = 0; col < colSize; col++)
{
int element;
element = twoDArray[row, col];
oneDArray[row * rowSize + col] = element;
}
}
}
static void DisplayOneDimArray(int[] oneDArray)
{
for (int i = 0; i < oneDArray.GetLength(0); i++ )
{
Console.Write("{0}\t", oneDArray[i] );
}
Console.WriteLine();
}
}
}
If you have a 2x5 array, your rowSize is 2, and your colSize is 5. Then your loop is setting a value into the array at [row * rowSize + col]. The first few values of this will be:
0*2+0 = 0
0*2+1 = 1
0*2+2 = 2
0*2+3 = 3
0*2+4 = 4
1*2+0 = 2
1*2+1 = 3
1*2+2 = 4
1*2+3 = 5
So you are looping over the same values, setting them multiple times, and also not setting the last values in the array. If you have more rows than columns, I imagine you would get an out of bounds exception?
If you change row * rowSize + col to the correct mapping row * colSize + col, it should work.
Try following code to copy to 1D:
void StoreValues(int[,] twoDArray, int[] oneDArray)
{
int rowSize = twoDArray.GetLength(0);
int colSize = twoDArray.GetLength(1);
var total=rowSize*colSize;//TOTAL ITEMS IN 1D ARRAY
for (int row = 0, d=0; row < rowSize; row++)
{
for (int col = 0; col < colSize; col++)
{
//STORE AT POSITION d
oneDArray[d++] = twoDArray[row, col];
}
}
}
using row * rowSize + col will cause issue as explained in this answer.