I tried to write a code that finds NashEquilibrium in given matrix.
I was keep getting errors that says I can't call non static method from static method so I turned every method and instance variable to static, is that a problem?
There are tons of logic errors in my code and it gives wrong answer, could it be because they are all static or its only logic error?
import java.util.ArrayList;
import java.util.Scanner;
public class Nash
{
public static String nes;
public static String str;
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the amount of strategies for each player");
int stratA = scan.nextInt();
int stratB = scan.nextInt();
String[][] utilities = new String[stratA][stratB];
System.out.println("Please enter the utilities");
for(int row = 0; row<stratA; row++)
for(int column = 0; column<stratB; column++)
utilities[row][column] = scan.next();
// Creates a 2D array with given utilities
if (nashExists(stratA, stratB, utilities) == true)
System.out.println(nes);
else
System.out.println("No NE found");
// Prints the results
}
public static boolean nashExists(int strA, int strB, String[][] util)
{
int[][] movesA = new int[strA][strB];
for(int row = 0; row<strA; row++)
for(int column = 0; column<strB; column++)
movesA[row][column] = Integer.parseInt(util[row][column].substring(0,1));
int[][] movesB = new int[strA][strB];
for(int row = 0; row<strA; row++)
for(int column = 0; column<strB; column++)
movesA[row][column] = Integer.parseInt(util[row][column].substring(2,3));
// Creates a 2d integer array for utilites of every strategy of A and B
ArrayList<String> aNE = new ArrayList<String>();
ArrayList<String> bNE = new ArrayList<String>();
for(int row = 0; row<strA; row++)
for(int column = 0; column<strB; column++)
if (nashExistsA(row, column, movesA) == true)
aNE.add((row+1) + "," + (column+1));
for(int row = 0; row<strA; row++)
for(int column = 0; column<strB; column++)
if (nashExistsB(row, column, movesB) == true)
bNE.add((row+1) + "," + (column+1));
// Checks if there are NE for one of players
if (compareArrayLists(aNE, bNE) == true)
return true;
else
return false;
}
// Checks if there are any matchs between both players NE's
public static boolean nashExistsA(int r, int c, int[][] a)
{
int max = a[r][c];
for (int i = 0; i<a.length; i++)
if (max < a[i][c])
max = a[i][c];
if (a[r][c] == max)
return true;
else
return false;
}
public static boolean nashExistsB(int r, int c, int[][] b)
{
int max = b[r][c];
for (int i = 0; i<b[0].length; i++)
if (max < b[r][i])
max = b[r][i];
if (b[r][c] == max)
return true;
else
return false;
}
public static boolean compareArrayLists(ArrayList<String> aN, ArrayList<String> bN)
{
for (int i=0; i<aN.size(); i++)
{
String potNE = aN.get(i);
if (bN.indexOf(potNE) >= 0)
str += "(" + potNE + ") ";
}
nes = str;
if (str.length()>0)
return true;
else
return false;
}
}