Solução com Validador de jogada

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.4 kB
1
Indexable
Never
#include <iostream>
#include <vector>
#include <math.h>

using namespace std;

vector < int >board = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sizeVector = board.size ();
int N = sqrt (board.size ());
  
void CalculateSolutions(vector<int>& board, vector<vector<int>>& horizontal, vector<vector<int>>& vertical, vector<vector<int>>& diagonal)
{
    cout << endl << "Solutions Vectors" << endl;

  for (int i = 0, x = 0; i < sizeVector; i += N)
    {
      for (int j = 0, y = 0, jinv = N - 1; j < N; j++, jinv--, y += N)
	{
	  horizontal[x][j] = board[i + j];
	  cout << "horizontal[" << x << "][" << j << "]= " << horizontal[x][j]
	    << " | ";
	  vertical[x][j] = board[x + y];
	  cout << "vertical[" << x << "][" << j << "]= " << vertical[x][j] <<
	    endl;
	  if (x == j)
	    {
	      diagonal[0][x] = board[i + j];
	    }
	  if (x == jinv)
	    {
	      diagonal[1][x] = board[i + j];
	    }
	}
      cout << endl;
      x++;
    }

  for (int i = 0; i < 2; i++)
    {
      cout << "diagonal[" << i << "]= ";
      for (int j = 0; j < N; j++)
	{
	  cout << diagonal[i][j] << " ";

	}
      cout << endl;
    }
}

int CompareVectorMatrix(vector<int>& playerMoves, vector<vector<int>>& solutionVector){
    for(int i=0; i<solutionVector.size(); i++)
    {
        if(playerMoves == solutionVector[i])
        {
            
            return 1;
        }
    }
    return 0;
}

void SolutionValidator(vector<int>& playerMoves, vector<vector<int>>& horizontal, vector<vector<int>>& vertical, vector<vector<int>>& diagonal){
    if(CompareVectorMatrix(playerMoves, horizontal) || 
        CompareVectorMatrix(playerMoves, vertical) || 
        CompareVectorMatrix(playerMoves, diagonal))
    {
        cout<<endl<<"You Win!!"<<endl;
    }
    else
    {
        cout<<endl<<"Player Moves does not match with any solution! :( "<<endl;
    }

}

int main ()
{
    vector<vector<int>> horizontal(N, vector<int>(N));
    vector<vector<int>> vertical(N, vector<int>(N));
    vector<vector<int>> diagonal(2, vector<int>(N));
  cout << endl << "Board: " << N << "x" << N << " | Vector Size: " <<
    sizeVector << endl;
  for (int i = 0; i < sizeVector; i += N)
    {
      for (int j = 0; j < N; j++)
	{
	  cout << board[i + j];
	}
      cout << endl;
    }
  
    
    CalculateSolutions(board, horizontal, vertical, diagonal);
    
    vector<int> player1 = {1,4,9};
    SolutionValidator(player1, horizontal, vertical, diagonal);


  return 0;
}