Matrix - Gun Game
Matrix - Gun Game
Zip the program as you solve each question and upload them separately in the
answers submission form.
1. Write a program to fill up the matrix size of 9*9 with 0 . Generate random number for
position x and position y and place '5' in that generated (x,y) position. Here, 5 is gun.
Note : Random number should be generated between 1 to 9. Assume matrix position
starts as (1,1)
Input :
0 - Filled matrix :
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Output :
Gun placed matrix :
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0
2. For the previous program, now add player '1', position him in the location by generating
'x' and 'y' by random number.
Note : random number generated for gun position (x,y) and player position (x,y)
should not be same. In case of generating same values, regenerate for new position. Player
1 denote as '1'
0 - Filled matrix :
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Placing Gun :
Generated random number for 'x' : 3
Generated random number for 'y' : 4
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
3. Moving operation : Now introduce moving functionality to the player. Finish the game
when reaching gun [ 5 ] position
1 - Choosing left direction
2 - Choosing right direction
3 - Choosing up direction
4 - Choosing down direction.
For each move generate number from 0-3 to move forward number of position. Forward
0 means no move.
0 - Filled matrix :
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Placing Gun :
Generated random number for 'x' : 3
Generated random number for 'y' : 4
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
4. Two Player game : Now introduce player '2' . Player who gets gun (reaching gun
position) is winner
Note : Generated position of player '2' should not be same as player '1' and gun [5]
position.
0 - Filled matrix :
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Placing Gun :
Generated random number for 'x' : 3
Generated random number for 'y' : 4
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Gun Holder : -
Placing player '2' :
Generated random number for 'x' : 1
Generated random number for 'y' : 4
0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
Gun Holder : -
Player '1' move :
Enter the direction you want to move : 3
Number of move (random number: 0-3) : 3
0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Gun Holder : -
Player '2' move :
Enter the direction you want to move : 4
Number of move (random number: 0-3) : 2
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
5. Add below functionality. Player who gets gun can shoot other person when
facing/meet opponent in the same row or column.
i) Player who gets gun should kill opponent in 3 tries (moves) , if so he is the
winner
ii) If player who gets gun was not able to shoot the opponent in 3 tries (moves),
then opponent is the winner.
iii) If player who gets gun is in same row/column in the opponent , before move
shooting is possible. For shooting , enter option '5'
Test case : follow the previous program (4) test case. So, now player '2' gets gun.
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
6. Now, Instead of finishing game, in case of missing opponent to shoot. If the player
who gets gun was not able to shoot the opponent, then gun is freed from the player and
will be placed randomly in some other places [ x,y - randomly generated position ]. Now,
both the player having chance to pick the Gun and game will be continued till some one
gets shooted.
Condition : Freed gun should be placed in new position which is within the radius
of distance 2 from the Player who is holding gun. Ex. Assume Player '1' holding gun at
position (3,4) and his tries are over. Now, Gun could be placed in following positions:
(1,2), (1,3,), (1,4), (1,5) , (1,6)
(2,2), (2,3,), (2,4), (2,5) , (2,6)
(3,2), (3,3), , (3,5), (3,6)
(4,2), (4,3,), (4,4), (4,5) , (4,6)
(5,2), (5,3,), (5,4), (5,5) , (5,6)
Test case : follow the program - 4 test case. So, now player '2' gets gun.
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0
As player '2' tries ( 3 moves over ), now gun is freed. So new position for gun in
radius 2 of Player '2'.
Gun Holder : -
Placing Gun :
Generated random number for 'x' : 5
Generated random number for 'y' : 3
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
2 0 5 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Gun Holder : Player 1
Player '1' move :
Enter the direction/shooting option : 1
Number of move (random number: 0-3) : 1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
2 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Test case :
0 - Filled matrix :
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Placing Gun :
Generated random number for 'x' : 3
Generated random number for 'y' : 4
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 6 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 6
Gun Holder : -
Player 1' Survival Power : 0
Player 2 Survival Power : 0
Gun Holder : -
Player 1' Survival Power : 1
Player 2 Survival Power : 0
and so on....