subroutine save_game(auxiliary_empty_cells,auxiliary_empty_cells_history,current_empty_cells_history,empty_cells,empty_cells_history,matrix,matrix_history,score_history)
	implicit none!all variables must be declared
	logical::file_exists=.false.!if true, the temporary file to store the matrix is present
	character(1000)::filename!the name of the file to save to
	character(1)::auxiliary_input='-'!used in case of overwriting
	integer(4),intent(inout)::auxiliary_empty_cells(0:255)!this vector keeps track of where the index of a certain cell is located in the empty_cells array, for example: empty_cells(0:3)=(/1,3,0,2/) would mean that the values of this array are the following: auxiliary_empty_cells(0:3)=(/2,0,1,3/), thus, only the index of a cell is needed when its interal data are required
	integer(4),intent(inout)::auxiliary_empty_cells_history(0:255,0:99)!same as matrix_history, but the player doesn't interact with this variable, it's only kept so it's faster to move back in the history of movements
	integer(4)::cells!number of cells per side of the square to be used in the game
	integer(4)::current_empty_cells!this is the amount of empty cells in a certain moment
	integer(4),intent(inout)::current_empty_cells_history(0:99)!same as matrix_history, but the player doesn't interact with this variable, it's only kept so it's faster to move back in the history of movements
	integer(4),intent(inout)::empty_cells(0:255)!the indeces of empty cells are sequentially kept here with no occupied cells in between, all of those are at the end of the array
	integer(4),intent(inout)::empty_cells_history(0:255,0:99)!same as matrix_history, but the player doesn't interact with this variable, it's only kept so it's faster to move back in the history of movements
	integer(4)::history_position!this variable keeps track of where should the program write in the history arrays
	integer(4)::i=0!first auxiliary index for the matrix
	integer(4)::j=0!second auxiliary index for the matrix
	integer(4)::k=0!third auxiliary index for the matrix_history array
	integer(4),intent(inout)::matrix(0:15,0:15)!all the number cells are here
	integer(4),intent(inout)::matrix_history(0:15,0:15,0:99)!history of player movements, when stored movements reach 99, it saves the next snapshot of the matrix in 0, overwriting the previous value, and starts overwriting the next ones as more snapshots are generated by the player
	integer(4)::score!this keeps the puntuation of the game
	integer(4),intent(inout)::score_history(0:99)!same as matrix_history, but the player doesn't interact with this variable, it's only kept so it's faster to move back in the history of movements
	integer(4)::snapshots!this is the amount of movements stored in the third index of matrix_history, when it reaches 100 it doesn't increase anymore
	common /basic/ cells,current_empty_cells,history_position,snapshots!sharing the variables needed for this subroutine except score
	common /score/ score!sharing the score variable too

	write(*,*) 'set title "Which file do you want to save to? Introduce its name or path."'!asking the player for the name of the file
	write(*,*) 'replot'!updating the output
10	read(*,*) filename!reading the input
	inquire(file=filename,exist=file_exists)!checking if the file is there
	if(file_exists)then
		write(*,*) 'set title "File already exists, do you want to overwrite it? (Just introduce y to overwrite or anything else to try a different name)"'!making sure nothing is unnecesarily overwritten
		write(*,*) 'replot'!updating the output
		read(*,*) auxiliary_input!reading the input
		if(auxiliary_input=='y')then
			open(20,file=filename,action='write',form='unformatted',access='stream',status='replace')!opening the file and overwriting it
		else
			write(*,*) 'set title "Try a different name."'!making sure nothing is unnecesarily overwritten
			write(*,*) 'replot'!updating the output
			go to 10!cycling through this if loop till a valid input is obtained
		end if
	else
		open(20,file=filename,action='write',form='unformatted',access='stream',status='new')!opening the new file
	end if
	write(20) cells,current_empty_cells,history_position,score,snapshots!reading the scalar variables
	do j=0,15
		do i=0,15
			write(20) matrix(i,j)!reading the main matrix
		end do
	end do
	do i=0,255
		write(20) auxiliary_empty_cells(i),empty_cells(i)!reading both empty cells arrays
	end do
	do k=0,99
		do j=0,15
			do i=0,15
				write(20) matrix_history(i,j,k)!reading the matrix_history array
			end do
		end do
	end do
	do j=0,99
		do i=0,255
			write(20) auxiliary_empty_cells_history(i,j),empty_cells_history(i,j)!reading both empty cells history arrays
		end do
	end do
	do i=0,99
		write(20) current_empty_cells_history(i),score_history(i)!reading the current empty cells and score history arrays
	end do
	close(20)!closing the file to save changes
	return
end subroutine save_game