public static void SendMessage(string user, string jsonString) { //populate oldBoard for (int i = 0; i < oldBoard.Length; ++i) { oldBoard[i] = new cell(); } //populate newBoard blank for (int i = 0; i < newBoard.Length; ++i) { newBoard[i] = new cell(); newBoard[i].life = 0; newBoard[i].color = "#000000"; } //add in cell data for the gameboard from the jsonString to oldBoard int count = 0; using (JsonDocument document = JsonDocument.Parse(jsonString)) { JsonElement root = document.RootElement; //JsonElement studentsElement = root.GetProperty("Students"); foreach (JsonElement cell in root.EnumerateArray()) { cell.TryGetProperty("life", out JsonElement lifeElement); oldBoard[count].life = lifeElement.GetInt32(); //Console.WriteLine(lifeElement.ToString()); cell.TryGetProperty("color", out JsonElement colorElement); oldBoard[count].color = colorElement.ToString(); //Console.WriteLine(colorElement.ToString()); count++; } } oldBoard[38].life = 1; oldBoard[38].color = "#ff34a4"; oldBoard[39].life = 1; oldBoard[39].color = "#00ffa4"; oldBoard[40].life = 1; oldBoard[40].color = "#ffffa4"; oldBoard[41].life = 1; oldBoard[41].color = "#ff0012"; oldBoard[60].life = 1; oldBoard[60].color = "#abff12"; oldBoard[79].life = 1; oldBoard[79].color = "#0000ff"; int x = 19; while (x < (18 * 17)) { if (x % 18 == 0) { x++; } else if (x % 18 == 17) { Console.Write("\n"); x++; } else { Console.Write(oldBoard[x].life); //Console.Write(x); ///check cell x++; } } checkBoard(oldBoard); Console.WriteLine(); //print board: ///////////////// x = 19; while (x < (18 * 17)) { if (x % 18 == 0) { x++; } else if (x % 18 == 17) { Console.Write("\n"); x++; } else { Console.Write(newBoard[x].life); //Console.Write(x); ///check cell x++; } } ///////////////// x = 19; while (x < (18 * 17)) { if (x % 18 == 0) { x++; } else if (x % 18 == 17) { Console.Write("\n"); x++; } else { Console.Write(newBoard[x].color); //Console.Write(x); ///check cell x++; } } ///receive board in json ///deserialize the board from json ///do board calculations on a new board ///serialize the new board back into json ///send to all clients //await Clients.All.SendAsync("ReceiveMessage", user, newBoard); }
static void Main(string[] args) { cell[] board2 = new cell[324]; for (int i = 0; i < board2.Length; ++i) { board2[i] = new cell(); board2[i].life = 0; board2[i].color = "#000000"; } /* * foreach (cell i in board2) * { * Console.WriteLine(i.life); * Console.WriteLine(i.color); * } */ board2[245].life = 1; string jsonString; jsonString = JsonSerializer.Serialize(board2); SendMessage("brad", jsonString); //Console.WriteLine(jsonString); //cell[] board = new cell[256]; //for (int i = 0; i < board.Length; ++i) //{ // board[i] = new cell(); //} //int count = 0; //using (JsonDocument document = JsonDocument.Parse(jsonString)) //{ // JsonElement root = document.RootElement; // //JsonElement studentsElement = root.GetProperty("Students"); // foreach (JsonElement cell in root.EnumerateArray()) // { // cell.TryGetProperty("life", out JsonElement lifeElement); // board[count].life = lifeElement.GetInt32(); // //Console.WriteLine(lifeElement.ToString()); // cell.TryGetProperty("color", out JsonElement colorElement); // board[count].color = colorElement.ToString(); // //Console.WriteLine(colorElement.ToString()); // count++; // } //} //foreach (cell i in board) //{ // Console.WriteLine(i.life); // Console.WriteLine(i.color); //} }