I want to make a game; say there are 50 turns and up to 4 players.
How should the code manage the turn by turn?
For 2 player I think it is that:
if (nbr_gamers == 2)
{
if ((turn % 2) == 0)
player = 1;
else
player = 0;
}
where the turn is the position of turn.
Is that about right?
Try this :
player = turn % nbr_gamers;
I'm doing homework for UNI and I got to do a Tic-Tac-Toe without any decision taken by player, the moves are all chosen randomly. So if the character on matrix is ' ' it means it's free, while if it's 'X' or 'O' it should generate another move. This is the code (language C):
if (playerTurn == 1){
playerSymb = 'X';
}
else if (playerTurn == 2){
playerSymb = 'O';
}
if (matrix[rand1][rand2] == ' '){
matrix[rand1][rand2] = playerSymb;
} else if(matrix[rand1][rand2] == 'X' || matrix[rand1][rand2] == 'O'){
do{
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}while (matrix[randAlt1][randAlt2] != 'X' && matrix[randAlt1][randAlt2] != 'O');
matrix[randAlt1][randAlt2] = playerSymb;
}
I did not copied the whole code because it's not finished at all, i just need help solving this. But if I try to run this, the Symbols can be overwritten, like if I have a 'X' at matrix[1][2], it's possible that it will be a 'O' after some turns. So how can I make moves do not overwrite? (sorry for bad english).
Just put correct condition:
while (matrix[randAlt1][randAlt2] == 'X' || matrix[randAlt1][randAlt2] == 'O')
(i.e. try again if this cell is not empty)
Also it is easy to simplify your code without loosing of anything:
randAlt1 = rand1;
randAlt2 = rand2;
while (matrix[randAlt1][randAlt2] != ' ') {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
And it is better to add loop guard to prevent infinite loop (or to add special checks for this case):
randAlt1 = rand1;
randAlt2 = rand2;
int nbAttempts = 0;
while (matrix[randAlt1][randAlt2] != ' ' && nbAttempts < 100) {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
nbAttempts++;
}
if (matrix[randAlt1][randAlt2] != ' ') {
// show error message and stop the game
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
You choose an arbitrary position and then test if it is free – possibly multiple times. But you can also choose a number of a free position and then find it.
First set up a turn counter
int turnNo = 0;
then make a loop for alternate moves, which chooses one of 9-turnNo unused positions, finds it, marks is with a player mark and tests if the move made a line of three:
while(turnNo < 9)
{
char currPlayerMark = ...choose 'X' or 'O';
int freePos = 9 - turnNo;
int currPos = rand() % freePos; // 0 .. freePos-1
for(x=0; x<3; x++)
{
for(y=0; y<3; y++)
{
if(matrix[x][y] == ' ') // a free position
if(--currPos < 0) // the sought one
break; // break the inner loop
}
if(currPos < 0)
break; // break the outer loop
}
matrix[x][y] = currPlayerMark;
if(test_for_win_position(x,y))
{
message_a_win_of_player(currPlayerMark);
break; // turnNo < 9 here
}
turnNo ++;
}
Finally test if the loop terminated with no 'win':
if(turnNo == 9)
message_its_a_draw(); // no-one wins
A function to test the win position might look like this:
int test_for_win_position(int x, int y)
{
char mark = matrix[x][y];
// check a column
if(matrix[x][0] == mark && matrix[x][1] == mark && matrix[x][2] == mark)
return 1;
// check a row
if(matrix[0][y] == mark && matrix[1][y] == mark && matrix[2][y] == mark)
return 1;
// check one diagonal
if(x==y)
if(matrix[0][0] == mark && matrix[1][1] == mark && matrix[2][2] == mark)
return 1;
// check another diagonal
if(x+y==2)
if(matrix[0][2] == mark && matrix[1][1] == mark && matrix[2][0] == mark)
return 1;
// current player has not won (yet)
return 0;
}
I need to write a program that returns the largest number entered by a user. The user can enter as many numbers >= 0 as they want and when they enter a -1, the program will stop and return the largest number. I am really struggling to do this. So far, I have this:
validInput = false;
while (~validInput)
fprintf('Enter a number >= 0 or a negative to quit.\n');
num = input('Enter a number or -1 to quit: ');
if(num == -1)
validinput = true;
counter = 0;
elseif(num>=0)
counter = counter+1;
end;
if(counter == 0)
fprintf('No values entered!');
else
array = (counter);
m = max(counter);
disp(m);
end
end``
enteredNumber = 0;
biggestNumber = 0;
while (enteredNumber ~= -1)
enteredNumber = input('Enter a number : ');
if enteredNumber > biggestNumber
biggestNumber = enteredNumber;
end
end
disp(['The biggest number entered is: ' num2str(biggestNumber)]);
You don't need to limit it to positive numbers but to answer your question, you can do this. And remove the || in < 0 to allow the user to choose negative numbers.
num = [];
while (true)
in = input('Enter a number or a non-numeric character to quit: ');
if isempty(in) || ~isnumeric(in) || in < 0
break
end
num(end+1) = in;
end
[M, INDEX] = max(num);
if ~isempty(num)
disp(['The ', num2str(INDEX),' was the maximum entered value and was: ', num2str(M), '.'])
else
disp('No value entered.')
end
if (turn == tick) {
/*first player*/
Form1->Label1->Caption = "X pyr";
fields[row][kol] = 1;
Form1->BitBtn1->Glyph->LoadFromFile("tick.bmp");
turn = tack;
}
else {
do {
//random
row = rand() % 3;
kol = rand() % 3;
}
while (fields[row][kol] == 0);
/*cpu*/
Form1->Label1->Caption = "CPU";
fields[row][kol] = 2;
Form1->BitBtn1->Glyph->LoadFromFile("tack.bmp");
turn = tick;
}
}
The main problem is that when I make my move, computer just clicks on first element and after every next move it does the same.
Computer just uses first TicTacToe game board square.
If i understand correctly the fields variable contain the board with 0 for unoccupied cell, 1 for human player, 2 for CPU.
In this case the terminal condition of the while is wrong while (fields[row][kol] == 0);, you must loop when the cell is occupied (trying to search for free cells).
do {
//random
row = rand() % 3;
kol = rand() % 3;
}
while (fields[row][kol] != 0);
Note: you are initializing all elements of fields to 0, that don't appear in the code.
CPU player loops until it finds a row and col value which is not equal to 0. do-while loop below loops if fields[row][col] is equal to 0 meaning after the exit fields[row][col] will be different than 0.
do {
...
} while(fields[row][col] == 0)
// fields[row][col] is different than 0 here
In your case field value not equal to 0 means a square already used by human or computer, so computer does the same move every time.
I have made this data.txt file and imported it into matlab using import wizard.
this is the result :
This table is my data base and I want to search into it column wise (each variable). I used strread as this lets me to search columns, for example :
var1=strread(VarName12(1,1),'%d',"delimiter','|')
I wrote several lines of code using strread in a script and then run it. when I check the workspace var1 and other variables that included strread result are missing and typing them in command window gives me this : Undefined function or variable 'End'. I typed sttread in command window and used variables in data.txt and got the result but it doesn't work when I run my script. I can't understand what's the problem? ( I'm using matlab R2013a and there is warning that says : using strread is not recommende. use TEXTSCAN instead. I can't do the same with textscan also strread works properly when written in workspace ). I don't know what is the problem. can give a clue?
EDIT :
this is the code
(Note: I'm writing a "biometric recognition system based on ear" and the database is data extracted from images1 to 5). This is the classification section of the code:
Nbcoordinates = vertcat(BEcell{:,1});
Necoordinates = vertcat(BEcell{:,2});
import DATA.txt.*; % import our data set
for i=1:5
token1 = 0; token2 = 0; token3 = 0; token4 = 0; token5 = 0; token6 = 0; token7 = 0;
if(VarName1(i, 1) == V{1,1}(1,2) && VarName3(i, 1) == V{1,2}(1,2) && VarName5(i,1) == V{1,3}(1,2) && VarName2(i,1) == V{1,1}(1,3) && VarName4(i,1) == V{1,2}(1,3) && VarName6(i,1) == V{1,3}(1,3))
% Check number of endings for each component
numberofendings = strread(VarName10{i,1}, '%d', 'delimiter', '|');
for j=1:size(Ne,1)
if(numberofendings(j,1)~=Ne(j,1))
break;
end
end
if(j >= size(Ne,1))
token1 = 1;
end
% check number of bifurcations for each components
numberofbifurcations = strread(VarName11{i,1}, '%d', 'delimiter', '|');
for j=1:size(Nb,1)
if(numberofbifurcations(j,1) ~= Nb(j,1))
break;
end
end
if(j >= Nb)
token2 = 1;
end
% Check Intersections1,2,3 Cordinates
Intercoordinate1 = strread(VarName7{i,1}, '%f', 'delimiter', '|');
m = 1;
for j=1:NI1
if((Intersection1(j, 1) ~= Intercoordinate1(m,1)) || (Intersection1(j, 2) ~= Intercoordinate1(m+1,1)))
break;
end
m = m + 2;
end
if(j >= NI1)
token3 = 1;
end
Intercoordinate2 = strread(VarName8{i,1}, '%f', 'delimiter', '|');
m = 1;
for j=1:NI2
if((Intersection2(j, 1) ~= Intercoordinate2(m,1)) || (Intersection2(j, 2) ~= Intercoordinate2(m+1,1)))
break;
end
m = m + 2;
end
if(j >= NI2)
token4 = 1;
end
Intercoordinate3 = strread(VarName9{i,1}, '%s', 'delimiter', '|');
m = 1;
for j=1:NI3
bi1 = cell2mat(Intercoordinate3(m, 1));
bi2 = cell2mat(Intercoordinate3(m + 1, 1));
if((Intersection3(j, 1) ~= str2num(bi2)) || (Intersection3(j, 2) ~= str2num(bi2)))
break;
end
m = m + 2;
end
if(j >= NI3)
token5 = 1;
end
% Check endings coordinate of each component
Endcoor = strread(VarName12{i,1}, '%d', 'delimiter', '|');
m = 1;
for j=1:sum(Ne)
en1 = Endcoor(m, 1);
en2 = Endcoor(m + 1, 1);
if((Necoordinates(j, 1) ~= en1) || (Necoordinates(j, 2) ~= en2))
break;
end
m = m + 2;
end
if(j >= sum(Ne))
token6 = 1;
end
% check bifurcation coordinates of each component
Bifcoor = strread(VarName13{i,1}, '%d', 'delimiter', '|');
m = 1;
for j=1:sum(Nb)
en1 = Bifcoor(m, 1);
en2 = Bifcoor(m + 1, 1);
if((Nbcoordinates(j, 1) ~= en1) || (Nbcoordinates(j, 2) ~= en2))
break;
end
m = m + 2;
end
if(j >= sum(Nb))
token7 = 1;
end
if(token1 ==1 && token2 == 1 && token3== 1 && token4 == 1 && token5 == 1 && token6== 1 && token7==1 )
disp(['This image is a member of Class ',num2str(i)]);
break;
end
end
end
if(token1 == 0 || token2 == 0 || token3 == 0 || token4 == 0 || token5 == 0 || token6== 0 || token7==0)
disp('Doesn;t exist in the data base ');
end
end