title: How can i ensure that moves sent over a network in a tic tac toe game does not overwrite the previous moves that were made
body: <p>I am creating a tic tac toe game that has networking capabilities. Everything is working fine except for one logic error that cannot seem to figure out. The issue is that when playing over a network, if player X makes a move then player O makes a move the previous move made by X get overridden my the move player O made. This does not happen when the came is being played without networking it for two players.</p>
  
  <p><img src="https://i.stack.imgur.com/hSSxT.png" alt="enter image description here"></p>
  
  <p>This is the method that controls who's turn it is and that sets the value X or O based on who's turn it is.</p>
  
  <pre><code>public MainForm()
      {
          InitializeComponent();
          display();
  
          network = new Network(this);
  
          btn = new Button();
  
      }
  
  private void btnClick(object sender, EventArgs e)
      {
  
          btn = (Button)sender;
  
          if (btn.Name == "button1")
          {
              col = 1;
              row = 1;
  
          }
          else if(btn.Name == "button2"){
              col = 2;
              row = 1;
  
          }else if(btn.Name == "button3"){
              col = 3;
              row = 1;
  
          }else if(btn.Name == "button4"){
              col = 1;
              row = 2;
  
          }else if(btn.Name == "button5"){
              col = 2;
              row = 2;
  
          }else if(btn.Name == "button6"){
              col = 3;
              row = 2;
  
          }else if(btn.Name == "button7"){
              col = 1;
              row = 3;
  
          }else if(btn.Name == "button8"){
              col = 2;
              row = 3;
          }
          else if (btn.Name == "button9")
          {
              col = 3;
              row = 3;
          }
  
  
          MakeMove(row, col);
  
      }
  
  public void MakeMove(int row, int col){
  
          if ((statusBar.Text == "Connecting...") ||
              (statusBar.Text == "Waiting for connection..."))
              return;
  
  
          //_____________________________________________________________________________________________
          //
          // Move network move
          //_____________________________________________________________________________________________
          //wServer = true;
  
          //MessageBox.Show("wServer is: " + wServer);
          if (((wServer == true) &amp;&amp; (turn == true) &amp;&amp; (isNetworkPlay == false)) ||
               ((wClient == true) &amp;&amp; (turn == false) &amp;&amp; (isNetworkPlay == false)))
          {
              //MessageBox.Show("retgr");
              network.SendMove(row, col);
          }
          else
          {
              if (((wServer == true) &amp;&amp; (turn == false) &amp;&amp; (isNetworkPlay == false)) ||
                  ((wClient == true) &amp;&amp; (turn == true) &amp;&amp; (isNetworkPlay == false)))
                  return;
          }
  
          //checks whos turn it is and sets the button to X or O
          //THIS IS WHERE I AM HAVING TROUBLE
          if (!turn)
          {
              //btn.Text = "X";
  
              SetControlPropertyValue(btn, "Text", "X");
  
  
          }
          else
          {
              //btn.Text = "O";
              SetControlPropertyValue(btn, "Text", "O");
  
          }
  
          turn = !turn;
          //btn.Enabled = false;
          SetControlPropertyValue(btn, "Enabled", false);
  
          turnCount++;
  
          //checks who wins
          checkWinner();
  
          display();
  
  
          isNetworkPlay = false;
      }![enter image description here][2]
  </code></pre>
  
  <p>In the networking class I have</p>
  
  <pre><code>  public void SendPacketTCP(Byte[] pDados)
      {
          //_____________________________________________________________________________________________
          //
          // Sends a packet via TCP
          //_____________________________________________________________________________________________
  
          try
          {
              if (objTicTacToe.wClient == true)
              {
                  if (clientSockStream == null)
                 {
                      MessageBox.Show("clientSockStream is NULL");
                      return;
                 }
  
  
                  if (clientSockStream.CanWrite)
                  {
                      MessageBox.Show("clientSockStream NOT NULL");
                      clientSockStream.Write(pDados, 0, 2);
                      clientSockStream.Flush();
                  }
              }
              else
              {
                  if (serverSockStream == null)
                  {
                      MessageBox.Show("serverSockStream is NULL");
                      return;
                  }
  
  
                  if (serverSockStream.CanWrite)
                  {
                      MessageBox.Show("serverSockStream NOT NULL");
                      serverSockStream.Write(pDados, 0, 2);
                      serverSockStream.Flush();
                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("An error ocurred: " + ex.Message + "\n" + ex.StackTrace);
              objTicTacToe.disconnect_Click(null, null);
              return;
          }
  
      }
  
      public void SendMove(int wRow, int wColumn)
      {
          //_____________________________________________________________________________________________
          //
          // Sends packet that shows move position
          //_____________________________________________________________________________________________
  
  
          MessageBox.Show("IN SEND MOVE. ROW is: " + wRow);
          MessageBox.Show("IN SEND MOVE. COL is: " + wColumn);
  
          byte[] buf = new byte[2];
          buf[0] = byte.Parse(Asc(wRow.ToString()).ToString());
          buf[1] = byte.Parse(Asc(wColumn.ToString()).ToString());
  
          //MessageBox.Show("IN SEND MOVE. Asc ROW is: " + Asc(wRow.ToString()).ToString());
          //MessageBox.Show("IN SEND MOVE. Asc COL is: " + Asc(wColumn.ToString()).ToString());
          //MessageBox.Show("IN SEND MOVE. BUF INDEX 0 is: " + buf[0]);
          //MessageBox.Show("IN SEND MOVE. BUF INDEX 1: " + buf[1]);
  
          SendPacketTCP(buf);
  
      }
  
  
    public void ConnectServer(string pIP)
      {
  
          //_____________________________________________________________________________________________
          //
          // Connect to a game server
          //_____________________________________________________________________________________________
  
          wServerIP = pIP;
          byte[] buf = new byte[1];
  
          thread_receive_client = new Thread(new ThreadStart(ThreadReceivingClient));
          thread_receive_client.Start();
  
      }
  
  
  private void ThreadReceivingClient()
      {
          //_____________________________________________________________________________________________
          //
          // Thread for receiving packets from server
          //_____________________________________________________________________________________________
  
          try
          {
  
              byte[] buf = new byte[512];
              int bytesReceived = 0;
  
              tcpClient = new TcpClient(wServerIP, SERVERPORT);
              clientSockStream = tcpClient.GetStream();
  
              objTicTacToe.clearBoard();
              objTicTacToe.setStatusMessage("Connected!");
  
              wReceivingClient = true;
  
              while (wReceivingClient)
              {
  
                  //_____________________________________________________________________________________________
                  //
                  // Thread is blocked until receives data
                  //_____________________________________________________________________________________________
  
                  try
                  {
                      bytesReceived = clientSockStream.Read(buf, 0, 2);
                  }
                  catch
                  {
                      return;
                  }
  
  
  
                  //_____________________________________________________________________________________________
                  //
                  // Processes network packet
                  //_____________________________________________________________________________________________
  
                  if (bytesReceived &gt; 0)
                  {
                      //_____________________________________________________________________________________________
                      //
                      // Control packet for game restart
                      //_____________________________________________________________________________________________
  
                      if (buf[0] == byte.Parse(Asc("R").ToString()))
                      {
                          objTicTacToe.clearBoard();
                          continue;
                      }
  
                      //_____________________________________________________________________________________________
                      //
                      // Packet indicating a game move
                      //_____________________________________________________________________________________________
  
                      int wRow = int.Parse(Convert.ToChar(buf[0]).ToString());
                      int wColumn = int.Parse(Convert.ToChar(buf[1]).ToString());
                      MessageBox.Show("IN ThreadReceivingClient. row is: " + wRow);
                      MessageBox.Show("IN ThreadReceivingClient. col is: " + wColumn);
  
                      if ((wRow &gt; 0 &amp;&amp; wRow &lt; 4) &amp;&amp; (wColumn &gt; 0 &amp;&amp; wColumn &lt; 4))
                      {
                          objTicTacToe.isNetworkPlay = true;
                          objTicTacToe.MakeMove(wRow, wColumn);
                          MessageBox.Show("IN ThreadReceivingClient. MOVE WAS SENT");
                      }
  
                  } //if (bytesReceived&gt;0) 
  
              } //while (wReceivingClient)
  
          }
          catch (ThreadAbortException) { }
          catch (Exception ex)
          {
              MessageBox.Show("An error ocurred: " + ex.Message + "\n" + ex.StackTrace);
              objTicTacToe.disconnect_Click(null, null);
              return;
          }
      }
  
  
  
   public void StartServer()
      {
  
          //_____________________________________________________________________________________________
          //
          // Starts game server
          //_____________________________________________________________________________________________
  
          thread_receive_server = new Thread(new ThreadStart(ThreadReceivingServer));
          thread_receive_server.Start();
      }
  
  
      private void ThreadReceivingServer()
      {
          //_____________________________________________________________________________________________
          //
          // Thread for receiving packets from client
          //_____________________________________________________________________________________________
  
  
  
          try
          {
              byte[] buf = new byte[512];
              IPHostEntry localHostEntry = Dns.GetHostByName(Dns.GetHostName());
              int bytesReceived = 0;
  
              tcpListener = new TcpListener(localHostEntry.AddressList[0], SERVERPORT);
  
              tcpListener.Start();
  
              //_____________________________________________________________________________________________
              //
              // Thread is blocked until it gets a connection from client
              //_____________________________________________________________________________________________
  
              soTcpServer = tcpListener.AcceptSocket();
  
              serverSockStream = new NetworkStream(soTcpServer);
  
              objTicTacToe.clearBoard();
              objTicTacToe.setStatusMessage("Connected!");
  
              wReceivingServer = true;
  
              //objTicTacToe.turn = false;
  
              while (wReceivingServer)
              {
  
                  //_____________________________________________________________________________________________
                  //
                  // Thread is blocked until receives data
                  //_____________________________________________________________________________________________
  
                  try
                  {
                      bytesReceived = serverSockStream.Read(buf, 0, 2);
                  }
                  catch
                  {
                      return;
                  }
  
                  //_____________________________________________________________________________________________
                  //
                  // Processes network packet
                  //_____________________________________________________________________________________________
  
                  if (bytesReceived &gt; 0)
                  {
  
                      //_____________________________________________________________________________________________
                      //
                      // Control packet for game restart
                      //_____________________________________________________________________________________________
  
                      if (buf[0] == byte.Parse(Asc("R").ToString()))
                      {
                          objTicTacToe.clearBoard();
                          continue;
                      }
  
  
  
                      //_____________________________________________________________________________________________
                      //
                      // Packet indicating a game move
                      //_____________________________________________________________________________________________
  
                      int wRow = int.Parse(Convert.ToChar(buf[0]).ToString());
                      int wColumn = int.Parse(Convert.ToChar(buf[1]).ToString());
                      MessageBox.Show("IN ThreadReceivingServer. row is: "+ wRow);
                      MessageBox.Show("IN ThreadReceivingServer. col is: " + wColumn);
  
                      if ((wRow &gt; 0 &amp;&amp; wRow &lt; 4) &amp;&amp; (wColumn &gt; 0 &amp;&amp; wColumn &lt; 4))
                      {
                          objTicTacToe.isNetworkPlay = true;
                          objTicTacToe.MakeMove(wRow, wColumn);
                          MessageBox.Show("IN ThreadReceivingServer. MOVE WAS SENT");
                      }
  
                  }   //if (bytesReceived&gt;0) 
  
              }   //while (wReceivingServer)
          }
          catch (ThreadAbortException) { }
          catch (Exception ex)
          {
              MessageBox.Show("An error ocurred: " + ex.Message + "\n" + ex.StackTrace);
              objTicTacToe.disconnect_Click(null, null);
              return;
          }
      }
  </code></pre>